繁体   English   中英

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

[英]Runtime.exec() : Reboot in Android?

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

我观察到,使用终端(例如adb shell或ConnectBot)中的重启来重启电话要比使用ACTION_REBOOT进行重启(无论如何我都无法使用)要快得多

目前,我可以通过以下方式获得超级用户特权:

Process root = Runtime.getRuntime().exec("su");

但我无法进行实际的重启。 我尝试使用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"); 

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

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

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

谢谢。

经过数周的搜寻,终于:

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

重启在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) {
                }
        }

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

此外,我可以使用Terminal Emulator应用程序在android手机上打开一个终端窗口,键入su获取#提示进行根访问,然后键入“ #reboot”,然后得到响应“不允许!”。

有什么建议么?

好的,没关系,我知道了。 在HTC手机上,即使具有SU根访问权限,重新启动命令也将不起作用。 需要调用BUSYBOX来执行重新启动命令。

该代码适用于Samsung Galaxy S2,S4,Sony 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;}
}

经过长期的奋斗,我找到了可行的解决方案。

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

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

如果使用普通端口,则使用下面的命令

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

区别仅在于/bin/xbin

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM