13

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

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

并添加了权限,REBOOT但它仍然不起作用。

谢谢

4

6 回答 6

35

这似乎对我有用:

try {
        Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
        proc.waitFor();
    } catch (Exception ex) {
        Log.i(TAG, "Could not reboot", ex);
    }
于 2011-11-11T23:50:05.293 回答
0

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

于 2015-03-07T16:39:17.313 回答
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.
    }
}
于 2014-09-18T17:16:19.357 回答
0

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

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
于 2017-09-27T14:57:17.387 回答
-1

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

try{
    Process p = Runtime.getRuntime().exec("su");
    OutputStream os = p.getOutputStream();                                       
    os.write("reboot\n\r".getBytes());
    os.flush();
}catch(IOException )
于 2012-10-26T22:59:47.593 回答
-4

如果手机root了,其实很简单:

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

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

于 2011-01-23T07:43:30.727 回答