我是一个stackoverflow的新手,所以我不能添加评论。
我看到了这个页面:
在su进程内读取命令输出
我尝试了这个答案,它可以:
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
但是当我在shell中尝试运行该命令时,响应仍然是相同的命令。
我输入了以下命令:
stdin.writeBytes("echo AT+CQI?\n");
答案是:
AT+CQI?
I wrote:
stdin.writeBytes("echo ATinkd\n");
答案是:
ATinkd
这意味着"bla..bla..bla.."。这意味着安卓系统无法将这些命令识别为AT命令。
我想知道是否有人有建议或解决方案。
\r
而不是\n
结尾。 - hlovdal