Runtime.getRuntime()。exec()的问题

时间:2011-08-02 06:38:51

标签: android

我已根据我的设备,然后在我的应用程序中

Process p = Runtime.getRuntime().exec("su");

它工作正常,我的应用程序将是root模式。然后我尝试添加wlan地址空间,但它不起作用,当我在终端检出时,显示以下错误信息

 busybox ifconfig there is not a new wlan address space. 

我尝试以下方式:

Process p = Runtime.getRuntime().exec("su");
p = Runtime.getRuntime().exec("busybox ifconfig wlan0 add xxxxxxxxxxxx");
p.waitfor();

当我运行我的应用程序时,toast显示应用程序是root模式但没有添加wlan0。

3 个答案:

答案 0 :(得分:2)

因为以“busybox”开头的进程并不相同 以“su”开头。你应该这样:

Process process = Runtime.getRuntime().exec("su");
OutputStream os = process.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeBytes("busybox ifconfig wlan0 add xxxxxxxxxxxx" + "\n");
dos.flush();

答案 1 :(得分:1)

实际上不支持“su -c COMMAND”语法。为了更好的可移植性,请使用以下内容:

p = Runtime.getRuntime().exec("su");
stream = p.getOutputStream();
stream.write("busybox ifconfig wlan0 add xxxxxxxxxxxx");

write()命令原样不存在,但我确定你会找到如何将流写入它,也许将输出流封装在BufferedOutputWriter中。

答案 2 :(得分:0)

这可能是因为当您运行su时,它会启动一个进程。然后运行busybox ...,它发生在另一个进程中,该进程不是以超级用户身份启动的。

尝试类似

的内容
Process p = Runtime.getRuntime().exec(new String[] {"su", "-c", "busybox ifconfig wlan0 add xxxxxxxxxxxx"});

,即在单个命令行中执行它。