Broadcast listening for Android boot and Shutdown
-
Android boot and shutdown broadcast listening
-
I. Boot broadcast Listening 1. register and receive the boot broadcast in the AndroidManifest. xml file
2. Execute what to do in BroadcastReceiver
Public class XXXBroadcastReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals (Intent. ACTION_BOOT_COMPLETED) {Intent newIntent = new Intent (context, XXX); newIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // note that this flag must be added; otherwise, context will fail to be started. startActivity (newIntent );}}}
Ii. listener for shutdown broadcast 1. Register with AndroidManifest. xml file to receive shutdown Broadcast
2. Execute what to do in BroadcastReceiver
Public class XXXBroadcastReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {// things to be handled when shutdown }}