Android Runtime.getRuntime().exec()导航目录

And*_*Jay 6 terminal command-line android runtime.exec

所以我希望能够编写一个可以打开并显示logcat消息,dmesg的应用程序,并且还能够运行像'ls''cat''echo''cd'这样的命令.

如果我执行以下操作:

nativeProc = Runtime.getRuntime().exec("ls\n");
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        String line = null;

        while ((line = in.readLine()) != null) {  
            full = full + "\n" + line;
        }
Run Code Online (Sandbox Code Playgroud)

我可以将文本"full"放到Text View中并查看根目录.

但是,这就是我所能做的一切.假设我想要找到一个目录,然后更改它,我遇到了麻烦.

所以,如果我这样做:

nativeProc = Runtime.getRuntime().exec("ls\n");
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        String line = null;

        while ((line = in.readLine()) != null) {  
            full = full + "\n" + line;
        }
        /* Code here to confirm the existing of a directory*/


        nativeProc = Runtime.getRuntime().exec("cd tmp\n");
        BufferedReader in2 = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        line = null;
        String test = "\nStart1:\n";
        while ((line = in2.readLine()) != null) {  
            test = test + "\n" + line;
        }
Run Code Online (Sandbox Code Playgroud)

"完整"和"文字"都没有任何结果

有任何想法吗?

mar*_*inj 4

您可以使用提供的路径执行 ls 来列出应列出的文件夹

Runtime.getRuntime().exec(new String[] { "ls", "\\tmp"});
Run Code Online (Sandbox Code Playgroud)