如何以编程方式重启手机? [英] How to Reboot phone programmatically?

查看:118
本文介绍了如何以编程方式重启手机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建应用程序(仅适用于Android 6),在该应用程序中,我必须使用按钮单击才能提供重新启动功能.我正在使用以下代码:

I am creating app(only for Android 6) in which I have to give reboot functionality using button click. I am using following code:

PowerManager pm =(PowerManager) getSystemService(Context.POWER_SERVICE);   
pm.reboot(null);

并添加了权限:

uses-permission android:name="android.permission.REBOOT",    
uses-permission android:name="android.permission.DEVICE_POWER" ,    
uses-permission android:name="android.permission.MODIFY_PHONE_STATE"

但出现以下错误:

java.lang.SecurityException:用户10298和当前进程都没有具有android.permission.REBOOT.

java.lang.SecurityException: Neither user 10298 nor current process has android.permission.REBOOT.

推荐答案

请尝试

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

OR

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

如果不起作用: Runtime.getRuntime().exec(new String [] {"su",-c",立即重启"}); 代替

您可能会使用PowerManager使其重新启动(这不能保证它将重新启动-操作系统可能会取消它):

You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):

http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)

重新启动设备.如果重新启动成功,将不会返回.需要REBOOT权限.

Reboot the device. Will not return if the reboot is successful.Requires the REBOOT permission.

http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

必须能够重新启动设备.不适用于第三方应用程序.常量值:"android.permission.REBOOT"

Required to be able to reboot the device. Not for use by third-party applications. Constant Value: "android.permission.REBOOT"

您不能通过普通的SDK应用程序执行此操作.只有使用系统固件签名密钥签名的应用程序才能执行此操作.

You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.

您需要使用系统固件密钥对您的应用进行签名.但是您的应用程序可能具有 Root 特权.尝试使用以下代码(如果您具有SU访问权限)

You need to sign your app with the System Firmware Key. But it's possible for your app with Root privileges. Try using the following code (if you have SU access):

关闭:

try {
    Process proc = Runtime.getRuntime()
                    .exec(new String[]{ "su", "-c", "reboot -p" });
    proc.waitFor();
} catch (Exception ex) {
    ex.printStackTrace();
}

重启:

相同的代码,只需使用"reboot" 而不是"reboot -p" .

Same code, just use "reboot" instead of "reboot -p".

这些不能在Stock HTC ROM上运行,但尚未确认自己身份

这篇关于如何以编程方式重启手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆