Differences between two Broadcast registration methods in Android programming
Resident Broadcast
Resident broadcast, when your application is closed, if broadcast information comes, the broadcast receiver you write can receive the same information, and its registration method is in AndroidManifast of your application. xml, which is also called static registration. This method can be understood as the broadcast registered through the list file is handed over to the operating system for processing. The sample code is as follows:
Very resident Broadcast
When the application ends, the broadcast will naturally disappear. For example, you can register the broadcast receiver in onCreate or onResume in the Activity and cancel the broadcast receiver in onDestory. In this way, your broadcast receiver is very resident. This registration method is also called Dynamic Registration. This method can be understood as the code-registered broadcast is associated with the registrant. For example, write a broadcast receiver that listens to the SDcard status:
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) {sup Er. 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 (sdc ArdStateReceiver);} class 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. println ("SDCard has been uninstalled! ");}}}}