我試圖以編程方式在Android設備上執行一些shell命令。我能夠運行一些命令但未能運行所有命令。舉個例子,我能夠運行以下命令:在Android Studio的ADB Shell中執行命令
executeCommandLine(「ls」)
executeCommandLine(「netstat –atun」)
,但現在我需要運行未正確執行以下命令:
$ adb push netstat3 /data/local/tmp/
$ adb shell
$ chmod 755 /data/local/tmp/netstat3
$ /data/local/tmp/netstat3
我寫信給執行上述功能Android中提到的命令。該函數爲我提供了像「ls」和「netstat -atun」這樣的命令的正確輸出,但是不會爲下一個命令提供正確的響應。我executeCommandLine功能如下:
public String executeCommandLine(String commandLine) {
try {
Process process;
process = Runtime.getRuntime().exec(commandLine);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String read;
StringBuilder output=new StringBuilder();
while ((read = reader.readLine())!=null){
output.append(read);
output.append("\n");
Log.d("executed command ", output.toString());
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
我想知道我可以爲我的所有命令的響應。
如果我沒有錯,你的意思是說你想在你的應用程序中的Android手機上以編程方式執行這些命令? –
是的,我想在我的應用程序中的Android手機上以編程方式執行。 – Bouba
你正試圖在應用程序內運行'adb push'? –