Android Studio 执行 su shell 并多次使用它以避免“su”吐司

发布于 2025-01-09 23:44:10 字数 1595 浏览 3 评论 0原文

我目前正在使用此代码发送 su 命令:

   private void execShellCmd(String cmd) {
    try {
        Process process = Runtime.getRuntime().exec("su");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}

在应用程序运行时期间,我需要执行多个命令,但使用我的代码,每次都会弹出 su toast 并阻止视图 3 秒。

如何使用 su 会话执行多个命令?

例如

private void initexecShell()
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void closeexecShell()

非常感谢您的帮助!

编辑:例如我尝试了以下方法,但不起作用:

private void test() {
initexecShell();
execShellCmd("command1");
//do something else
execShellCmd("command2");
//do something
execShellCmd("command3");
closeexecShell();

}

Process process;
private void initexecShell()
{
    Process process = Runtime.getRuntime().exec("su");
}
     
private void execShellCmd(String cmd) {
    try {
        
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}
private void closeexecShell()
{
   //?? no idea 
}

I`m currently using the this code to send su commands:

   private void execShellCmd(String cmd) {
    try {
        Process process = Runtime.getRuntime().exec("su");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}

During App Runtime i need to execute several commands, but with my code the su toast popup every time and is blocking view for 3 seconds.

How can I use the su session for multiple commands?

e.g.

private void initexecShell()
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void execShellCmd(String cmd)
private void closeexecShell()

Thanks a lot for you help!

Edit: for example I tried the following, but does not work:

private void test() {
initexecShell();
execShellCmd("command1");
//do something else
execShellCmd("command2");
//do something
execShellCmd("command3");
closeexecShell();

}

Process process;
private void initexecShell()
{
    Process process = Runtime.getRuntime().exec("su");
}
     
private void execShellCmd(String cmd) {
    try {
        
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes(cmd);
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {

    }
}
private void closeexecShell()
{
   //?? no idea 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2025-01-16 23:44:10
 private void test()
{
    try {
        SystemClock.sleep(5000); // ignore sleep, just for testing
        //It is important to apply for root permission, otherwise it will be useless
        Process process = Runtime.getRuntime().exec("/system/bin/sh");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes("su\n");
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        dataOutputStream.writeBytes("exit\n");
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {
        output1.setText(output1.getText() + "\n" + e.getMessage());
    }
}
 private void test()
{
    try {
        SystemClock.sleep(5000); // ignore sleep, just for testing
        //It is important to apply for root permission, otherwise it will be useless
        Process process = Runtime.getRuntime().exec("/system/bin/sh");
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeBytes("su\n");
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        SystemClock.sleep(1000);
        dataOutputStream.writeBytes("echo \"hello world\" > /sdcard/log.txt\n");
        dataOutputStream.writeBytes("exit\n");
        dataOutputStream.flush();
        dataOutputStream.close();
        outputStream.close();
    } catch (Throwable e) {
        output1.setText(output1.getText() + "\n" + e.getMessage());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文