Runtime.exec():在Android中重启?

Rob*_*Rob 16 java android runtime reboot root

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

我观察到重新启动手机远比使用终端(adb shell或ConnectBot)中的重启要快得多,而不是通过使用ACTION_REBOOT重新启动,这无论如何都无法使用.

目前,我可以获得超级用户权限

Process root = Runtime.getRuntime().exec("su");
Run Code Online (Sandbox Code Playgroud)

但我不能做实际的重启.我尝试G1(HTC)和Galaxy S(三星)没有任何成功.我找到了重启可执行文件/system/bin/reboot

以下是我的一些尝试:

Process reboot = Runtime.getRuntime().exec("/system/bin/reboot");
Process reboot = Runtime.getRuntime().exec("reboot");
Process reboot = Runtime.getRuntime().exec("su reboot"); 
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢.

Kev*_*ker 34

经过数周的搜索后,最后:

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

  • 出于某种原因,我不得不使用:Runtime.getRuntime().exec(new String [] {"su"," - c","rebo​​ot now"}); 相反,但它奏效了.非常感谢! (5认同)
  • 谢谢,我喜欢这个小巧的! (2认同)

jkh*_*uw1 15

重启在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) {
                }
        }
Run Code Online (Sandbox Code Playgroud)