问题描述
我试图运行
String command = "su -c 'busybox ls /data'";
p = Runtime.getRuntime().exec(command);
在我的应用程序,但它似乎是语法是某种错误的。我没有问题,运行它从终端仿真器应用程序在手机上,虽然如此,我只是不明白为什么它不工作从我的应用程序调用的时候。
in my app, but it seems like the syntax is somehow wrong. I have no problem running it from the terminal emulator app on the phone, though, so I just can't understand why it is not working when called from within my app.
任何帮助表示深深的AP preciated!
Any help is deeply appreciated!
推荐答案
解决方案找到了!由于该链接通过它身上的。见code以下:超级用户shell命令正常工作,你首先需要创建一个超级用户的外壳,并将其分配给一个过程,然后写入和读取它的输入和输出流分别为
SOLUTION FOUND! Thanks to the link suggested by onit here. See the code below: for superuser shell commands to work properly, you first need to create a superuser shell and assign it to a process, then write and read on it's input and output streams respectively.
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
//from here all commands are executed with su permissions
stdin.writeBytes("ls /data\n"); // \n executes the command
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[BUFF_LEN];
int read;
String out = new String();
//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
while(true){
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read<BUFF_LEN){
//we have read everything
break;
}
}
//do something with the output
这篇关于语法在Android应用程序shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!