2013-04-09 68 views
2

我是Android新手。我试图运行一个shell命令来重命名系统中的文件。我有root权限。如何在Android应用程序中运行重命名shell命令(已过滤)

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"); 
} 
+1

我确定有错误信息或类似的东西......你为什么不分享你的问题? – mthmulders 2013-04-09 10:26:18

回答

4

您可以使用相同的过程,通过写命令运行一个以上的命令进程的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(); 
+0

@cyanide否,'getOutputStream()'返回连接到子进程正常输入的输出流。输出到流被输入到由此Process对象表示的流程的标准输入中。请参阅[javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getOutputStream()) – dan 2013-04-09 10:45:18

+0

哇这是工作非常感谢 – Mahendra 2013-04-09 10:53:20

+0

@ user2244000不客气。你可以接受它有帮助的答案:)。 – dan 2013-04-09 12:13:20

0

你需要每个命令要在同一进程中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会成功。这里的“正确途径”不同于一个制造商,但可以通过编程收集。

相关问题