[英]Obtain the permission of shutdown or restart in android
I am creating a shutdown/restart function. 我正在创建一个关机/重启功能。
Intent intent = new Intent();
intent.setAction("android.intent.action.ACTION_SHUTDOWN");
sendBroadcast(intent);
In xml: 在xml中:
<uses-permission android:name="android.permission.SHUTDOWN"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.crazyit.net"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system">
But it has this error in users-permission: permission is only granted to system apps
. 但它在users-permission中有此错误: permission is only granted to system apps
。
Any idea? 任何想法?
As the error message explains, a normal app cannot get this permission. 正如错误消息所解释的那样,普通应用无法获得此权限。 It would be a security risk since you could build an application that renders the phone useless. 这将是一个安全风险,因为您可以构建一个使手机无用的应用程序。
Normal SDK applications cannot cause phones to shut down or reboot. 普通的SDK应用程序无法导致手机关机或重启。 Only applications that are part of the firmware (ie, signed by the firmware sining key) can hold the proper permission and perform those actions. 只有属于固件的应用程序(即由固件主题密钥签名)才能保留适当的权限并执行这些操作。
In android SDK documents, It is clearly stated that the ACTION_SHUTDOWN
and ACTION_REBOOT
are protected intents that can only be sent by the system". You don't have the privilege to use this intent to reboot the device for security reason. 在android SDK文档中,明确指出ACTION_SHUTDOWN
和ACTION_REBOOT
是受保护的意图,只能由系统发送“。出于安全原因,您无权使用此意图重启设备。
In my case i want to launch my app on mobile switch on and set its status on mobile switched off, So for that i use in this way 在我的情况下,我想在移动开关上启动我的应用程序并在移动设备关闭时设置其状态,所以为此我用这种方式
<receiver android:name="WifiNotification" android:enabled="true" android:permission="android.permission.CHANGE_WIFI_STATE">
<intent-filter >
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.SCAN_RESULTS"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
and in my WifiNotification i did :: 在我的WifiNotification中我做了::
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)
{.
.
.
}
else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_SHUTDOWN))
{.
.
.
}
}
One of the permission is :: 其中一个许可是::
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
If your device is rooted, you can use this code for shutdown 如果您的设备已植根,则可以使用此代码进行关闭
Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot -p" });
and use this code for Reboot 并使用此代码重新启动
Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });
I use this on Xamarin.Android 我在Xamarin.Android上使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.