Obtain the permission of shutdown or restart in an

2019-01-28 21:13发布

问题:

I am creating a shutdown/restart function.

    Intent intent = new Intent();  
    intent.setAction("android.intent.action.ACTION_SHUTDOWN");  
    sendBroadcast(intent); 

In 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.

Any idea?

回答1:

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.



回答2:

Normal SDK applications cannot cause phones to shut down or reboot. Only applications that are part of the firmware (i.e., 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.



回答3:

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 ::

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"/>


回答4:

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