首先,我将介绍我的情况。 我需要执行" su"命令在我的Android应用程序中,它运作良好。然后我需要执行" ls"命令并读取输出。我是通过从" su"获取输出流来实现的。处理并将命令写入其中。
这就是问题所在。如何阅读" ls"的输出?处理?我所拥有的只是" su"过程对象。从中获取输入流没有任何结果,因为" su"没有写任何东西。但是" ls"我不知道如何访问其输出消息。
我搜索了很多网站,但我找不到任何解决方案。也许有人会帮助我:)。
此致
答案 0 :(得分:24)
好的,我找到了解决方案。它应该是这样的:
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
希望它会对某人有所帮助
答案 1 :(得分:5)
public String ls () {
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
catch (IOException e) {}
return output;
}
检查此处提及的代码:
How to run terminal command in Android application?
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
<强>参考强>
答案 2 :(得分:2)
我修改了@glodos接受的以下问题的答案:
ps
(即adb shell
)
在几次执行之后,您将看到几个su
进程
活。他们需要妥善终止。waitFor()
以确保流程终止。read=-1
的处理,现在可以执行空stdout
的命令。之前他们在new String(buffer, 0, read)
使用StringBuffer
进行更有效的字符串处理。
private String execCommand(String cmd) throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
DataOutputStream stdout = new DataOutputStream(p.getOutputStream());
stdout.writeBytes(cmd);
stdout.writeByte('\n');
stdout.flush();
stdout.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(p.getInputStream()));
char[] buffer = new char[1024];
int read;
StringBuffer out = new StringBuffer();
while((read = stdin.read(buffer)) > 0) {
out.append(buffer, 0, read);
}
stdin.close();
p.waitFor();
return out.toString();
}
有些学分归@Sherif elKhatib))