我在清单中设置了权限:
<uses-permission android:name="android.permission.REBOOT" />
我调用以下线路重新启动设备:
Intent intent = new Intent(Intent.ACTION_REBOOT);
sendBroadcast(intent);
由于我在模拟器中具有root权限,因此我很好奇为什么会遇到以下错误:
Permission Denial: not allowed to send broadcast android.intent.action.REBOOT from pid=963, uid=10046
REBOOT 权限仅授予具有平台签名或系统应用程序的应用程序。
作为root可能允许你以root身份运行命令,但你的应用程序仍然不是。 (不过,你可以sudo调用关闭命令)
如果有人仍在寻找并且我们确实在谈论有根设备,请阅读此答案。
以下代码示例应该执行此操作(根据请求添加):
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.
}
}