【问题标题】:Android - can't execute simple cd in emulatorAndroid - 无法在模拟器中执行简单的 cd
【发布时间】:2015-02-03 14:33:59
【问题描述】:
  • 我有一个植根的模拟器(虽然我不确定它是 相关)

  • 我正在尝试为我的论文构建一个根应用程序,即当按钮 按下时,它会尝试查找数据库文件。

  • 我已经到了尝试从 在应用程序中(java代码),但我能做的就是ls

  • 当我尝试 cd 到另一个文件夹时,它不起作用。在cd之后 我执行pwdls 仍然会为/ 提供结果。

  • 下面是我的代码的相关部分

    Button btn_read = (Button) findViewById(R.id.button);
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*String s;
    
                try {
                    Process p = Runtime.getRuntime().exec("ls -al");
                    p = Runtime.getRuntime().exec("adb shell");
                    p = Runtime.getRuntime().exec("cd data/data");
    
                    BufferedReader stdInput = new BufferedReader(new
                            InputStreamReader(p.getInputStream()));
    
                    BufferedReader stdError = new BufferedReader(new
                            InputStreamReader(p.getErrorStream()));
    
                    // read the output from the command
                    Log.d("ReadDB", "Here is the standard output of the command:\n");
                    while ((s = stdInput.readLine()) != null) {
                        Log.d("ReadDB", s);
                    }
    
                    // read any errors from the attempted command
                    Log.d("ReadDB", "Here is the standard error of the command (if any):\n");
                    while ((s = stdError.readLine()) != null) {
                        Log.d("ReadDB", s);
                    }
    
                    System.exit(0);
                }
                catch (IOException e) {
                    Log.d("ReadDB", "Exception happened - here's what I know: ");
                    e.printStackTrace();
                    System.exit(-1);
                }*/
    
                executeCommand(new String[] {
                    "sh", "-c", "ls -l"
                });
                /*executeCommand(new String[] {
                    "sh", "-c", "cd /mnt"
                });*/
                executeCommand(new String[] {
                    "sh", "-c", "ls -l data/data"
                });
                executeCommand(new String[] {
                    "sh", "-c", "pwd"
                });
            }
        });
    }
    
    private static void executeCommand(String[] commands) {
        Process p;
        try {
            p = Runtime.getRuntime().exec(commands);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            String line = "";
            while ((line = reader.readLine())!= null) {
                Log.d("ReadDB", line + "\n");
            }
            Log.d("ReadDB", "--------------------------------------------------------------------------------------");
    
        } catch (Exception e) {
            Log.d("ReadDB", "Exception occured");
            e.printStackTrace();
        }
    }
    

谁能帮忙?

【问题讨论】:

  • 谢谢!它似乎正在工作

标签: java android shell command emulation


【解决方案1】:

这些命令将在单独的subshells 中运行,因此这是不可能的。 应该做什么:

executeCommand(new String[] {
    "sh", "-c", "cd /mnt && some other command"
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2017-03-20
    • 2017-05-18
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多