The difference between two ways of registering a broadcast under Android programming

Source: Internet
Author: User

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

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.