Android Study Notes 40: broadcastreceiver of four Android Components

Source: Internet
Author: User

Android applicationsProgramThe four components are activity, service, broadcastreceiver, and contentprovider. Activity, service, and contentprovider have been described in previous blog posts, as follows:

(1) activity: Android Study Notes 38: Activity of four Android Components

(2) Service: Android Study Notes 39: Service of four Android Components

(3) contentprovider: Android Study Notes 37: Using content providers to share data

This article mainly introduces broadcastreceiver.

 

1. System broadcast

Broadcastreceiver is a component responsible for receiving and responding to messages. Like a service, broadcastreceiver does not provide a UI interface for user interaction.

There are many standard broadcast actions in the Android system, including the following:

(1) intent. action_camera_button; // press the photo button

(2) intent. action_data_changed; // The device date has changed.

(3) intent. action_time_changed; // the set time is changed.

(4) intent. action_timezone_changed; // time zone changed

(5) intent. action_boot_completed; // The system has been started.

(6) intent. action_package_added; // The APK is successfully installed.

(7) intent. action_package_changed; // The installed APK is changed.

(8) intent. action_package_removed; // Delete the APK

(9) intent. action_package_data_cleared; // clear application data

(10) intent. action_batiery_low; // low battery

(11) intent. action_batiery_changed; // Changes in battery power in the charging status

(12) intent. action_power_connected; // connect to the external power supply

(13) intent. action_power_disconnected; // disconnect the external power supply.

(14) intent. action_headset_plug; // insert a headset

(15) intent. action_input_method_changed; // The input method is changed.

(16) intent. action_media_checking; // insert an external storage device for Detection

(17) intent. action_media_mounted; // The external storage device is mounted successfully.

(18) intent. action_media_removed; // remove an external storage device.

(19) intent. action_screen_off; // screen off

(20) intent. action_screen_on; // the screen is displayed.

(21) intent. action_shutdown; // shut down the system

These are some of the broadcast actions built in the Android system. We can directly receive these broadcast actions in our applications and handle them according to the different broadcasts we receive.

 

2. Publish Broadcast

In addition to the built-in broadcast of the Android system described above, it is easy to create and publish a custom broadcast in our own applications.

The broadcast content and the action used to filter the broadcast are carried out in the intent object, so we can add the broadcast content to the intent object through the putextra () method provided by the intent class, the setaction () method provided by the intent class is used to add the filtering broadcast action to the intent object. The specific implementation method is as follows:CodeAs shown in:

 1       /*  2   * Function: Click Event processing.  3   * Author: blog Park-still indifferent  4        */  5       Public   Void  Onclick (view ){ 6           Switch  (View. GETID ()){  7           Case  R. Id. Button:  8 Intent intent = New Intent (); //  Create an intent object  9 Intent. setaction (my_action ); //  Set the action attribute of the intent object  10 Intent. putextra ("key_msg", medittext. gettext (). tostring ()); //  Add broadcast content to the intent object  11 Sendbroadcast (intent ); //  Send Broadcast  12               Break  ;  13   }  14 }

In the above Code, we added the filter broadcast action to the intent object through the setaction () method. This action is customized, just like the built-in broadcast action in Android, this action can be used to uniquely identify custom broadcasts. Here I will define this action as follows:

Private Static FinalString my_action = "com. example. android_broadcast.msg ";

In addition, we add broadcast content to the intent object through the putextra () method. The broadcast content is the input content in the edittext control. The putextra () method provides key-value pairs for data storage.

Finally, we can use the sendbroadcast () method to send intent objects with broadcast information.

Note that the context class provides three broadcast sending methods:

(1) Context. sendbroadcast ();

(2) Context. sendstickybroadcast ();

(3) Context. sendorderedbroadcast ();

Among them, intent sent out using sendbroadcast (), all broadcastreceiver that meets the condition will execute its onreceive () method, but if there are multiple broadcastreceiver that meets the condition, the order in which the onreceive () method is executed is not certain.

Intent sent by sendstickybroadcast () always exists. If you call the registerreceive () method to register a broadcastreceiver that meets the conditions, the intent object is directly returned to the newly registered broadcastreceiver.

Intent sent by sendorderedbroadcast () method will execute the onreceive () method based on the priority set by intentfilter during broadcastreceiver registration, but the broadcastreceiver with the same priority will execute onreceive () the order of methods is not certain.

 

3. Receive Broadcast

If we need to receive a broadcast (Android built-in broadcast or custom broadcast) in our application and process the broadcast we receive, then we need to create a broadcastreceiver class (inherited from Android. content. broadcastreceiver class) and implements the onreceive () method of the broadcastreceiver class. The onreceive () method in the broadcastreceiver class is called when it receives the broadcast sent to itself.

In addition, we also need to register the broadcastreceiver class we created. There are two registration methods: one is in androidmanifest. register broadcastreceiver in the XML file, and register broadcastreceiver directly in the code using the registerreceive () method.

The following describes how these two methods are implemented.

3.1 register broadcastreceiver in the androidmanifest. xml file

First, we need to create a broadcastreceiver class to receive and process the broadcast information just released, as shown in the following code:

 1  /*  2   * Class: broadcastreceiver class, used to receive and process broadcast information  3   * Author: blog Park-still indifferent  4  */  5  Public   Class Mybroadcastreceiver Extends  Broadcastreceiver {  6  7   @ Override  8  Public   Void  Onreceive (context, intent ){  9 String MSG = intent. getstringextra ("key_msg "); //  Obtain broadcast content  10 Toast. maketext (context, "the received broadcast information is:" + MSG, Toast. length_long). Show ();  11   }  12  13 }

Through the above Code, we created a broadcastreceiver class named "mybroadcastreceiver", removed the broadcast content in the onreceive () method, and displayed it.

After coding mybroadcastreceiver, you must register this component in the androidmanifest. xml file. The specific method is as follows:

 1      <  Cycler  2           Android: Name  = ". Mybroadcastreceiver"  >  3           <  Intent-Filter  >  4               <  Action  Android: Name  = "Com. example. android_broadcast.msg"      /> 5           </  Intent-Filter  >  6       </  Cycler  > 

<Receiver ER> </receiver ER> labels are nested in <Application> </Application> labels. Android: Name = ". mybroadcastreceiver" specifies the name of the broadcastreceiver component to be registered. The <action> label in the <intent-filter> </intent-filter> label specifies the filter rules of the broadcastreceiver component. Android: Name = "com. example. android_broadcast.msg" in the tag is the broadcast action we previously defined. Therefore, the unique broadcast action that mybroadcastreceiver wants to respond to is determined.

3.2 register broadcastreceiver using registerreceive ()

In the activity, we can also register broadcastreceiver directly in the onstart () method by calling the registerreceiver () method, and in the onstop () method by calling unregisterreceiver () method to cancel registration of broadcastreceiver, as shown in the following code:

 1       /*  2   * Function: onstart () method  3   * Author: blog Park-still indifferent  4        */  5       Protected   Void  Onstart (){  6          Super  . Onstart ();  7 Intentfilter = New  Intentfilter ();  8 Intentfilter. addaction (my_action ); //  Set action  9 Registerreceiver (mybroadcastreceiver, intentfilter ); //  Register the broadcastreceiver component  10   } 11   12       /*  13   * Function: onstop () method  14   * Author: blog Park-still indifferent  15        */  16       Protected   Void  Onstop (){  17 Unregisterreceiver (mybroadcastreceiver ); // Unregister the broadcastreceiver component  18           Super  . Onstop ();  19 }

Note that if you register broadcastreceiver in the onresume () method of the activity, you need to cancel the registration of broadcastreceiver in the onpause () method of the activity.

3.3 differences between two Broadcast registration methods

The broadcastreceiver registered in the androidmanifest. xml file is a resident broadcast. Even if the application is closed, the created broadcastreceiver can receive broadcast information. Therefore, this is a form of static registration broadcast.

Broadcastreceiver registered using the registerreceive () method is a very resident broadcast. Generally, the unregisterreceiver () method is called to remove broadcastreceiver when the activity ends. Therefore, this is a form of Dynamic Registration broadcast.

 

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.