【问题标题】:How to run rename shell command in android app (rooted)如何在android应用程序中运行重命名shell命令(根)
【发布时间】:2013-03-31 17:36:59
【问题描述】:

我是 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");
}

【问题讨论】:

  • 我确定有错误消息或类似的东西......你为什么不在你的问题中分享呢?

标签: java android eclipse shell command


【解决方案1】:

您需要每个命令与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/<proper path here> /system 会成功。 “此处的正确路径”因制造商而异,但可以通过编程方式收集。

【讨论】:

    【解决方案2】:

    您可以使用同一进程运行多个命令,方法是将命令写入进程的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();
    

    【讨论】:

    • @cyanide 否,getOutputStream() 返回连接到子进程正常输入的输出流。流的输出通过管道传输到此 Process 对象表示的进程的标准输入中。见javadoc
    • @user2244000 不客气。您可以接受它有帮助的答案:)。
    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多