大家都知道在不ROOT的情况下一般是无法实现一键关机和重启的。但是想要实现还是有办法的,那就是在系统的源码下编译APK使其获得系统权限,这样就能执行关机和重启命令了,其核心思想是把我们的APK进程添加到system,提升权限,OK,其实也不需要源码,有系统的签名密钥就可以了,实现步骤如下:
1.编写好相关代码
private void DoReboot()
{
Intent intent = new Intent(Intent.ACTION_REBOOT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void DoShutdown()
{
Intent intent =new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
2.在AndroidManifest里添加属性sharedUserID
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
android:sharedUserId="android.uid.system"
...>
3.eclipse生成APK,删掉META-INF目录下的CERT.SF和CERT.RSA两个文件
4.使用当前android系统的platform密钥来重新给apk文件签名。首先,找到密钥文件,在我的Android源码目录中的位置是"build\target\product\security",下面的platform.pk8和platform.x509.pem两个文件。然后,用Android提供的SignApk工具来签名,signapk的源代码是在"build\tools\signapk"下。
5.签名方法java -jar signapk.jar platform.x509.pem platform.pk8 xxx.apk xxxSigned.apk
6.安装生成的xxxSigned.apk,便可以实现关机和重启了