如何在android app中运行rename shell命令(rooted)
我是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 /system\n"); out.writeBytes("mv /system/file.old system/file.new\n"); out.writeBytes("exit\n"); out.flush(); process.waitFor();
您需要每个命令与su
在同一个进程中,因为切换到root不适用于您的应用程序,它适用于su
,它在您开始mount
之前完成。
相反,尝试两个exec:
...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/
会成功。 这里的“正确路径”与制造商不同,但可以编程方式收集。