gpt4 book ai didi

java - Android - 无法在模拟器中执行简单的 cd

转载 作者:行者123 更新时间:2023-12-01 12:10:20 25 4
gpt4 key购买 nike

  • 我有一个已root的模拟器(尽管我不确定它是相关)

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

  • 我已经尝试从以下位置执行 shell 命令在应用程序中(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();
    }
    }

有人可以帮忙吗?

最佳答案

这些命令将在单独的 subshells 中运行,所以这是不可能的。什么应该有效:

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

关于java - Android - 无法在模拟器中执行简单的 cd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com