我試圖通過我的應用程序運行3個不同的命令,但只有第一個執行。 這是代碼。如何通過android中的應用程序運行多個shell命令
Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("mount -o remount,rw /system");
process = Runtime.getRuntime().exec("cp /sdcard/hosts /system/etc");
我得到root訪問權限,但之後沒有其他事情發生。
編輯:我想這個代碼,但這樣也只能執行蘇COMAND
String[] commands = {"mount -o remount,rw /system", "cp /sdcard/hosts /system/etc"};
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
編輯:這在時間工作,但只需要一個命令,我不得不做出一個按鈕,每一個命令。
String[] hin1 = { "su", "-c","cp /sdcard/Mediafire/hosts /system/etc/" };
try {
Runtime.getRuntime().exec(hin1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
可能重複(http://stackoverflow.com/questions/6882248/running-shell-commands-though [運行殼牌雖然在Android Java代碼指令] -java-code-on-android) –