13

首先我将介绍我的情况。我需要在我的 android 应用程序中执行“su”命令,它运行良好。然后我需要执行“ls”命令并读取输出。我通过从“su”进程获取输出流并将我的命令写入其中来做到这一点。

问题来了。如何读取“ls”进程的输出?我所拥有的只是“su” Process 对象。从中获取输入流什么都没有,因为“su”不写任何东西。但是“ls”可以,我不知道如何访问它的输出消息。

我搜索了很多网站,但没有找到任何解决方案。也许有人会帮助我:)

问候

4

3 回答 3

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

希望对某人有所帮助

于 2011-08-05T08:24:50.940 回答
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;
}

检查这里提到的代码:

如何在 Android 应用程序中运行终端命令?


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

参考

这段代码 GScript

于 2011-08-01T10:12:13.573 回答
3

我针对以下问题修改了@glodos 接受的答案:

  1. 流关闭,否则 exec 进程将永远挂起,在打开的流上。如果您在多次执行后ps在 shell(即adb shell)中执行,那么您将看到几个su进程处于活动状态。他们需要被适当地终止。
  2. 添加waitFor()以确保进程终止。
  3. 添加了对 的处理,现在可以执行read=-1带空的命令。stdout以前他们坠毁在new String(buffer, 0, read)
  4. 用于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 ))

于 2017-11-03T00:13:48.060 回答