Detailed explanation of BroadcastReceiver components in Android

Source: Internet
Author: User

Detailed explanation of BroadcastReceiver components in Android

The four components of the Android system finally have one component, BroadcastReceiver, which isGlobal listenerTo monitor global broadcast messages of the system.Communication between different components in the system

BroadcastReceiver has its own process and system-level listener. As long as a matching Intent is broadcast, BroadcastReceiver will be inspired to create its own BroadcastReceiver object. We need to inherit from android. content. broadcastReceiver and its onReceive Method

Mycycler. java

Public class myincluer extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {Toast. makeText (context, "the received Intent Action is:" + intent. getAction () + "\ n message content:" + intent. getStringExtra ("msg"), Toast. LENGTH_LONG ). show ();}}
The aggreger configured in the Manifest. xml configuration file
 
  
 
That is to say, no matter which component, the intent Action is "org. crazyit. action. CRAZY_BROADCAST" and uses sendBroadcast (intent) for broadcasting, then the myaggreger will be started.
Public class BroadcastMain extends Activity {Button send; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the send = (Button) findViewById (R. id. send); send. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// create Intent object Intent intent = new Intent (); // set Intent Action attribute intent. setAction ("org. crazyit. action. CRAZY_BROADCAST "); intent. putExtra ("msg", "simple message"); // send broadcast sendBroadcast (intent );}});}}

There are two ways to register a consumer Er:

Static Registration

Static registration is configured in the AndroidManifest. xml file, and we will register a broadcast address for MyReceiver:

               
                                      
                 
   
            
  
After the above information is configured, as long as it is broadcast of the android. intent. action. MY_BROADCAST address, MyReceiver can receive it. Note that this method of registration is resident. That is to say, when the application is closed, if the broadcast information is sent, MyReceiver will be called by the system and run automatically.

Dynamic Registration

Dynamic Registration requires you to dynamically specify the broadcast address and register it in the Code. Generally, we register a broadcast in Activity or Service. Let's take a look at the registered code:

MyReceiver receiver = new MyReceiver (); IntentFilter filter = new IntentFilter (); filter. addAction ("android. intent. action. MY_BROADCAST "); registerReceiver (receiver, filter); // register
Receiver and filter

 

 
Normal Broadcast 

Normal broadcast is completely asynchronous for multiple receivers. Generally, each receiver can receive the broadcast without waiting. The receiver does not affect each other. For such broadcast, the receiver cannot terminate the broadcast, that is, it cannot block the receiving actions of other receivers. The above example is the normal broadcast sent.

Ordered Broadcast

Ordered broadcast is special. It is only sent to the receiver with a higher priority each time, and then the receiver with a higher priority spreads to the receiver with a lower priority. The receiver with a higher priority can terminate the broadcast.

For example, Priority A> B> C, Broadcast first passes to A, then to B, and then to C. Priority statement In the element android: priority, the higher the number, the value range is-1000 ~ 1000

The receiver who receives the Broadcast message first can store the processing result in Broadcast using the setResultExtras (Bundle) method, then pass it to the next receiver, and use Bundle bunde = getResultExtras (true) ke to obtain the data stored by the previous receiver.

Public class SortedBroadcast extends Activity {Button send; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // get the send Button in the program send = (Button) findViewById (R. id. send); send. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// create Intent object Intent intent = new Intent (); intent. setAction ("org. crazyit. action. CRAZY_BROADCAST "); intent. putExtra ("msg", "simple message"); // send ordered broadcast sendOrderedBroadcast (intent, null );}});}}
Mycycler. java
Public class myincluer extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {Toast. makeText (context, "the received Intent Action is:" + intent. getAction () + "\ n message content:" + intent. getStringExtra ("msg"), Toast. LENGTH_LONG ). show (); // create a Bundle object and store the data Bundle bundle = new Bundle (); bundle. putString ("first", "the message that the first BroadcastReceiver stores"); // put the bundle into the result setResultExtras (bundle ); // cancel the continuous Broadcast of Broadcast // abortBroadcast (); // ① }}
MyReceiver2.java
Public class MyReceiver2 extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {Bundle bundle = getResultExtras (true ); // parse the message String first = bundle in which the key of the previous BroadcastReceiver is first. getString ("first"); Toast. makeText (context, "the first Broadcast stored message is:" + first, Toast. LENGTH_LONG ). show ();}}
Configuration File
 
  
 
 
  
 

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.