Resident broadcast
Resident broadcast, when your application is closed, if there is a broadcast message, the broadcast receiver you write can receive the same, and it is registered in the Androidmanifast.xml of your application, which is often referred to as static registration. This approach can be understood as registering a broadcast through a manifest file is given to the operating system to handle. The sample code is as follows:
<!--subscribe to end broadcast--><receiver android:name= ". Receiver. Bootcompletereceiver "> <intent-filter android:priority=" "> <action android:name=" Android.intent.action.BOOT_COMPLETED "/> </intent-filter></receiver>
Non-resident broadcast
Very resident broadcast, when the application is over, Broadcast nature is not, for example, in the Activity of onCreate or Onresume register the broadcast receiver, in the ondestory to write off the broadcast recipient. This way your broadcast receiver is a very resident type, which is also called dynamic registration. This approach can be understood as registering a broadcast through code that is associated with a registrant. For example, write a broadcast receiver that listens to the SDcard state:
Package Cn.sunzn.mosecurity.activity;import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.os.environment;public class SDcard extends Activity {sdcardstatechanagereceiver Sdcardstatereceiver; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Sdcardstatereceiver = new Sdcardstatechanagereceiver (); Intentfilter filter = new Intentfilter (); Filter.addaction (intent.action_media_removed); Filter.addaction (Intent.action_media_eject); Filter.addaction (intent.action_media_mounted); Filter.adddatascheme ("file"); Registerreceiver (sdcardstatereceiver, filter); } protected void OnDestroy () {unregisterreceiver (sdcardstatereceiver); } class Sdcardstatechanagereceiver extends Broadcastreceiver {public void OnReceive (context context, Intent Intent) {Checksdcard (); public void Checksdcard () {String state = Environment.getexternalstoragestate (); System.out.println (state); if (state.equals (environment.media_removed) | | state.equals (environment.media_unmounted)) {System.out.print ln ("SDcard unloaded!"); } } }}
The difference between two ways of registering a broadcast under Android programming