我试图通过我的应用程序运行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) –