Android essay--android broadcast mechanism broadcast detailed

Source: Internet
Author: User

In Android, some operations will be sent after the broadcast, such as sending a text message, or a phone call, if a program received the broadcast, will do the appropriate processing. This broadcast has something in common with the radio broadcasts in our traditional sense. It is called broadcasting because it is only responsible for "saying" regardless of whether you "listen", that is, regardless of how your receiver handles it. In addition, the broadcast can be received by more than one application, and of course it may not be received by any application.

One, the Android broadcasting mechanism three elements:

1. Broadcast (broadcast): Used to send broadcasts. is a widely used mechanism for transmitting information between applications.

2. Broadcast Receiver (Broadcastreceiver): Used to receive broadcasts. is a component that is filtered to receive and respond to the emitted broadcast.

3. Intent content (Intent): Medium for storing broadcast-related information

Second, the function and characteristics of broadcasting:

1, the broadcast life cycle is very short, after the call object-implementation onreceive-end of the entire process is over. From the complexity of implementation and the amount of code, broadcast is undoubtedly the most mini Android component, the implementation often only a few lines of code. When a broadcast object is constructed, it usually executes only the Broadcastreceiver.onreceive method and ends its life cycle. So sometimes we can think of it as a function or not.

2, like all components, the broadcast object is also constructed in the main thread of the application process, so the execution of the broadcast object must be synchronous and fast. It is also not recommended to open a sub-thread inside, because the thread is not finished, and the broadcast object has been executed and destroyed by the system. If you need to complete a more time-consuming task, you should do so by sending Intent to the service.

3. Each time the broadcast arrives, the Broadcastreceiver object is recreated, and the OnReceive () method is called, and the object is destroyed when it finishes executing. When the OnReceive () method is not completed within 10 seconds, Android will consider the program unresponsive.

Third, the two forms of registration of broadcasting:

1, resident broadcast: Resident broadcast, when your application is closed, if there is broadcast information, you write a broadcast receiver can also receive, it is registered in the Androidmanifast.xml of your application is registered, this method of registration 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:

<receiverAndroid:name=". Alarmreceiver " ><!--reveiver Name, if the internal class statically registers the broadcast, add $ before the inner class -    <Intent-filter>        <ActionAndroid:name= "Android.intent.action.ALARM_RECEIVER" /><!--broadcast received by intent -        <categoryAndroid:name= "Android.intent.category.DEFAULT" />    </Intent-filter></receiver>

Create a new alarmreceiver, inherit Broadcastreceiver

 Public class extends broadcastreceiver{    @Override    publicvoid  onreceive (Context arg0, Intent arg1) {        "I am the alarm clock, I want to wake you up ...", Toast.length_short). Show ();      
}

Finally create a simple activity on the inside, send a static broadcast on the line

1  PackageCom.example.alarmmanager;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 7  Public classMainactivityextendsActivity {8 9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -  -         //send a static broadcast theIntent Intent =NewIntent ("Android.intent.action.ALARM_RECEIVER"); - Sendbroadcast (intent); -     } -  +}

2, very resident broadcast: Very resident broadcast, when the application is over, broadcast nature is not, such as in the Activity of onCreate or Onresume register the broadcast receiver, in 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:

1  PackageCom.example.alarmmanager;2 3 Importandroid.app.Activity;4 ImportAndroid.content.BroadcastReceiver;5 ImportAndroid.content.Context;6 Importandroid.content.Intent;7 ImportAndroid.content.IntentFilter;8 ImportAndroid.os.Bundle;9 Importandroid.os.Environment;Ten  One  Public classMainactivityextendsActivity { A sdcardstatechanagereceiver Sdcardstatereceiver; -  -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); -  -         //Create a new Broadcastreceiver -Sdcardstatereceiver =Newsdcardstatechanagereceiver (); +         //Add Intent Filter -Intentfilter filter =NewIntentfilter (); +         //add an action to filter to indicate that only the broadcasts of these actions are received A filter.addaction (intent.action_media_removed); at filter.addaction (intent.action_media_eject); - filter.addaction (intent.action_media_mounted); -         //setting SDcard Plug and receive -Filter.adddatascheme ("File"); -         //registering a dynamic broadcast - Registerreceiver (sdcardstatereceiver, filter); in     } -  to     protected voidOnDestroy () { +         //when destroying activity, be sure to remember to cancel the broadcast, or it will be an error - Unregisterreceiver (sdcardstatereceiver); the     } *  $     //BroadcastreceiverPanax Notoginseng     classSdcardstatechanagereceiverextendsBroadcastreceiver { -          Public voidOnReceive (Context context, Intent Intent) { theString State =environment.getexternalstoragestate (); + System.out.println (state); A             if(State.equals (environment.media_removed) the||state.equals (environment.media_unmounted)) { +System.out.println ("SDcard has been uninstalled!")); -             } $         } $     } -}

Sky Road

Reprint please indicate source: http://www.cnblogs.com/travellife/

Android essay--android broadcast mechanism broadcast detailed

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.