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

由网友(揽月)分享简介:如何通过Android应用程序发送一个命令到终端并获得输出反馈?例如,发送LS /和获取输出到GUI打印?How to send a command to the terminal through android app and get the output back? For example, sending "l...

如何通过Android应用程序发送一个命令到终端并获得输出反馈?例如,发送LS /和获取输出到GUI打印?

How to send a command to the terminal through android app and get the output back? For example, sending "ls /" and getting the output to print it in the GUI?

推荐答案

您必须使用反射来调用android.os.Exec.createSubprocess():

You have to use reflection to call android.os.Exec.createSubprocess():

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;
}
阅读全文

相关推荐

最新文章