我是Android新手。我正在尝试运行shell命令来重命名系统中的文件。我有root权限。
shell命令:$ su
# mount -o remount,rw /system
# mv system/file.old system/file.new
我已经试过了,但是不工作:
public void but1(View view) throws IOException{
Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("mount -o remount,rw /system");
process = Runtime.getRuntime().exec("mv /system/file.old system/file.new");
}
通过在进程的OuputStream
中写入命令,可以在同一个进程中运行多个命令。这样,这些命令将在运行su
命令的相同上下文中运行。比如:
Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("mount -o remount,rw /systemn");
out.writeBytes("mv /system/file.old system/file.newn");
out.writeBytes("exitn");
out.flush();
process.waitFor();
您需要每个命令与su
处于相同的进程中,因为切换到根并不适用于您的应用程序,它适用于su
,在您到达mount
之前完成。
相反,尝试两个执行命令:
...exec("su -c mount -o remount,rw /system");
...exec("su -c mv /system/file.old system/file.new");
另外,请注意,我看到过一些系统,mount -o remount,rw /system
会失败,而mount -o remount,rw /dev/<proper path here> /system
会成功。"此处的正确路径"在不同的制造商之间是不同的,但它可以通过编程收集。