【问题标题】:Runtime.exec() : Reboot in Android?Runtime.exec() :在 Android 中重新启动?
【发布时间】:2011-07-25 23:12:16
【问题描述】:

我正在寻找一种可用于重新启动已root 设备的解决方案。我知道重启设备对于用户as stated here 来说是非常糟糕的设计,而且它并不是真正的应用程序。主要目的是在我的测试期间重启手机(我在一个视频聊天应用程序上工作,有时我需要在一切都向南时重启)

我观察到,在终端(例如adb shell 或 ConnectBot)中使用 reboot 重启手机比使用 ACTION_REBOOT 重启手机要快得多,因为无论如何我都无法使用。

目前,我可以通过

获得超级用户权限
Process root = Runtime.getRuntime().exec("su");

但我无法进行实际的重启。我尝试了 G1 (HTC) 和 Galaxy S (Samsung),但没有成功。我在/system/bin/reboot中找到了重启可执行文件

这是我的一些尝试:

Process reboot = Runtime.getRuntime().exec("/system/bin/reboot");
Process reboot = Runtime.getRuntime().exec("reboot");
Process reboot = Runtime.getRuntime().exec("su reboot"); 

我阅读了this article 关于 Runtime.exec() 的缺陷,但我认为我不是这种情况。

由于使用 ConnectBot 使我能够执行这样的操作,我很确定这是可能的。请不要告诉我去看看ConnectBot code,这是一个大而复杂的项目:)

你能帮我解决这个问题吗?

谢谢。

【问题讨论】:

标签: java android runtime root reboot


【解决方案1】:

经过长时间的努力,我找到了可行的解决方案。

如果你的系统使用的是串口,那么执行下面的命令,

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});

如果使用普通端口,则执行以下命令

Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot now"});

区别只是/bin/xbin

如果第一个命令抛出异常然后执行第二个,那么可以这样编写代码。

【讨论】:

  • Runtime.getRuntime().exec(new String[]{"su","-c","re​​boot now"});似乎对这两种情况都有效
  • 无法运行程序“su”:错误=13,权限被拒绝
【解决方案2】:

此代码适用于三星 Galaxy S2、S4、索尼 Xperia S (LTi 26)

public static void rebootDevice()
{
    try 
    {           
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("reboot \n");
    } 
    catch (Throwable t) {t.printStackTrace;}
}

【讨论】:

  • 无法运行程序“su”:错误=13,权限被拒绝
【解决方案3】:

我发现我无法以编程方式重新启动。

此外,我可以使用终端模拟器应用在我的安卓手机上打开一个终端窗口, 输入 su 获取 # 提示以获取 root 访问权限 然后输入“#reboot” 我得到“不允许!”的响应!

有什么建议吗?

好吧,没关系,我想通了。 在 HTC 手机上,即使使用 SU root 访问,重启命令也不起作用。 需要调用 BUSYBOX 来执行重启命令。

【讨论】:

  • 如果有人在 HTC 手机上遇到同样的问题,有一种更简单的方法可以绕过“不允许!”消息,无需安装 Busybox:无需调用“rebo​​ot”命令,只需通过调用“echo b > /proc/sysrq-trigger”将“b”写入 /proc/sysrq-trigger
【解决方案4】:

经过数周的搜索终于:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});

【讨论】:

  • 谢谢,我喜欢这款小巧的!
  • 由于某种原因我不得不使用:Runtime.getRuntime().exec(new String[]{"su","-c","re​​boot now"});相反,但它奏效了。非常感谢!
  • 它在我的三星设备上不工作,我需要给予任何许可吗?
  • @DavidShelabarger:这是因为su 可能在/system/xbin/ 下,而不是/system/bin/。该位置取决于您获得su 的方式或您的设备植根方式。
  • 相关设备需要root。
【解决方案5】:

reboot 在 android 中运行良好。您可能没有正确执行 runtime.exec() 。 你需要处理

    public static void rebootSU() {
    Runtime runtime = Runtime.getRuntime();
    Process proc = null;
    OutputStreamWriter osw = null;
    StringBuilder sbstdOut = new StringBuilder();
    StringBuilder sbstdErr = new StringBuilder();

    String command="/system/bin/reboot";

    try { // Run Script

        proc = runtime.exec("su");
        osw = new OutputStreamWriter(proc.getOutputStream());
                            osw.write(command);
                osw.flush();
        osw.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();                    
            }
        }
    }
    try {
        if (proc != null)
            proc.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
            .getInputStream())));
    sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
            .getErrorStream())));
    if (proc.exitValue() != 0) {
                }
        }

【讨论】:

  • 感谢完整的代码,我没有很好地使用进程。
  • 无法运行程序“su”:错误=13,权限被拒绝
  • @GauravMandlik 你在这上面找到什么了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多