17

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

我观察到,在终端(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"); 

我读过这篇关于 Runtime.exec() 陷阱的文章,但我认为我不是这种情况。

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

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

谢谢。

4

5 回答 5

36

终于经过数周的搜索:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
于 2011-04-24T06:05:30.740 回答
16

重启在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) {
                }
        }
于 2011-04-07T16:10:59.957 回答
1

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

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

有什么建议么?

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

于 2012-05-11T17:29:26.600 回答
1

此代码适用于三星 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;}
}
于 2014-07-17T07:07:25.133 回答
0

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

如果您的系统使用串行端口,则执行以下命令,

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

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

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

区别只是/bin/xbin

所以可以像第一个命令抛出异常然后执行第二个那样的代码。

于 2016-08-11T07:28:57.417 回答