Differences between two Broadcast registration methods in Android programming

Source: Internet
Author: User

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! ");}}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.