Android 2.2:以编程方式重启设备

时间:2011-01-02 20:08:14

标签: android reboot

我想知道是否有办法通过代码重启设备。我试过了:

Intent i = new Intent(Intent.ACTION_REBOOT); 
i.putExtra("nowait", 1); 
i.putExtra("interval", 1); 
i.putExtra("window", 0); 
sendBroadcast(i);

并为REBOOT添加了权限,但它仍无效。

由于

6 个答案:

答案 0 :(得分:35)

这似乎对我有用:

try {
        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
        proc.waitFor();
    } catch (Exception ex) {
        Log.i(TAG, "Could not reboot", ex);
    }

答案 1 :(得分:0)

仍然对于有根设备,但是如果你想要更安全(process.waitFor()有条件,在单独的try-catch中,我们有适当的异常处理,“现在”在重启后添加到命令中,这对于某些设备是必需的,等等,也许更干净的代码,看看这个:

Process rebootProcess = null;
try
{
    rebootProcess = Runtime.getRuntime().exec("su -c reboot now");
}
catch (IOException e)
{
    // Handle I/O exception.
}

// We waitFor only if we've got the process.
if (rebootProcess != null)
{
    try
    {
        rebootProcess.waitFor();
    }
    catch (InterruptedException e)
    {
        // Now handle this exception.
    }
}

答案 2 :(得分:0)

您可以使用PowerManager重启(这并不能保证它会重启 - 操作系统可能会取消它): links
link #2

答案 3 :(得分:0)

我正在使用Xamarin。对我来说,解决方案是:

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });

答案 4 :(得分:-1)

这是一个解决方案。请记住,设备必须扎根。

try{
    Process p = Runtime.getRuntime().exec("su");
    OutputStream os = p.getOutputStream();                                       
    os.write("reboot\n\r".getBytes());
    os.flush();
}catch(IOException )

答案 5 :(得分:-4)

如果手机有根,那实际上非常简单:

try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}               

第一个命令将要求获得超级用户权限。第二,将重启手机。 清单文件中不需要额外的权限,因为实际的重新启动是由执行的comamand而不是应用程序处理的。