我想知道是否有办法通过代码重新启动设备。我试过了:
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
并添加了权限,REBOOT
但它仍然不起作用。
谢谢
这似乎对我有用:
try {
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
proc.waitFor();
} catch (Exception ex) {
Log.i(TAG, "Could not reboot", ex);
}
仍然适用于有根设备,但如果您想要更安全(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.
}
}
我正在使用 Xamarin。对我来说,解决方案是:
Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
这是一个解决方案。请记住,设备必须植根。
try{
Process p = Runtime.getRuntime().exec("su");
OutputStream os = p.getOutputStream();
os.write("reboot\n\r".getBytes());
os.flush();
}catch(IOException )
如果手机root了,其实很简单:
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}
第一个命令将请求超级用户权限。第二,将重启手机。清单文件中不需要额外的权限,因为实际的重启是由执行的命令处理的,而不是应用程序。