代码 -
public class ShutdownReceiver extends BroadcastReceiver {
private static final String TAG = "ShutdownReceiver";
@Override
public void onReceive(final Context context, final Intent intent) {
Logger.i(TAG, "Shutting Down..........................");
if("android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {
//Power Off
}
}
}
服务-
private BroadcastReceiver mReceiver = null;
@Override
public void onCreate() {
super.onCreate();
// INITIALIZE RECEIVER
//It is used to register broadcast for ShutDown or Power Off
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
mReceiver = new ShutdownReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
}
权限 -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.DEVICE_POWER" />