开发者

Problem with Runtime.getRuntime().exec()

I have rooted my device, then in my application

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

and it work fine, my application will be root mode. Then I try add wlan address space, but it doesn't work, when I check out in terminal, following error 开发者_运维技巧message is shown

 busybox ifconfig there is not a new wlan address space. 

I try with following way:

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

When I run my application, the toast shows that the app is root mode but there is not added wlan0.


The "su -c COMMAND" syntax is not really supported. For better portability, use something like this:

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

The write() command doesn't exists as-is, but I'm sure you'll find how to write your stream to it, maybe encapsulating the output stream in a BufferedOutputWriter or so.


because the process that started with "busybox" is not the same one which started with "su". you should like this:

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();


That might be because when you run su, it launches one process. Then you run busybox ..., and it happens in another process, which is not started as a superuser.

Try something like

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

, i.e. executing it in a single command-line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜