Android Learning Note (12) Broadcastreceiver ordered broadcast and priority

Source: Internet
Author: User
Tags terminates

The first two posts simply tidy up the ordinary broadcast, in fact, there is an orderly broadcast, orderly broadcast in the development is also more than the minimum, you can set priority for the broadcast receiver to control the order of acceptance, but can interrupt broadcast transmission and so on.

One or two kinds of broadcast:

  · General broadcast (normal broadcast): sent by using the Sendbroadcast () method.

The general broadcast is completely asynchronous and can logically be received by all matching receivers at the same time, with a high efficiency of message delivery, with the disadvantage that the recipient cannot pass the processing result to the next recipient or terminate the broadcast propagation.

  · Ordered broadcast (Ordered broadcast): Sent with the Sendorderedbroadcast () method.

The receivers of ordered broadcasts will receive the priority of advance life, the higher the number of priority (the value range: -1000~10000), the priority can be declared in <intent-filter android:priority= "n" .../> You can also call the SetPriority setting of the Intentfilter object. And the receiver can terminate the propagation (call the Abortbroadcast () method to terminate), and the receiver cannot accept the broadcast once it terminates. In addition, the recipient can deposit the processing results into the data (Setresultextras (bundle) method can be stored in the data broadcast), as broadcast to the next level of recipients (through the code bundle bundle = Getresultextras (True) gets the data passed at the previous level).

SMS interception principle: The system receives the text message, the broadcast belongs to the orderly broadcast, the program can receive the notification by setting priority, then terminates the delivery.

Two, for example (ordered broadcast):

1, first create two broadcastreceiver. Interrupts the first receive after it has been received for broadcast.

Myreceiver.java

 1  public  class  Myreceiver extends   Broadcastreceiver { Span style= "color: #008080;" >2  3   @Override  4  public  void   OnReceive (context context, Intent Intent) { 5  System.out.println ("MyR Eceiver received the message " 6  abortbroadcast (); //  interrupt the broadcast, no more than it has a priority low broadcast and then propagate down the  7   8  }

Myreceiver1.java

1  Public class extends broadcastreceiver {23    @Override4      public  void  OnReceive (context context, Intent Intent) {5         System.out.println (" MyReceiver1 received the message "); 6     }7 }

2, then the two receiver action in the Androidmanifest.xml file is configured as the same, and set to a different priority, the code is as follows:

1 <receiverAndroid:name=". Myreceiver ">2             <!--Priority: The higher the number, the higher the priority level -3             <Intent-filterandroid:priority= "5" >4                 <ActionAndroid:name= "Com.codingblock.myreceiver.intent.action.MyReceiver"/>5             </Intent-filter>6         </receiver>7         <receiverAndroid:name=". MyReceiver1 ">8             <Intent-filterandroid:priority= "4">9                 <ActionAndroid:name= "Com.codingblock.myreceiver.intent.action.MyReceiver"/>Ten             </Intent-filter> One         </receiver>

3. Finally send the broadcast in the Mainactivity

1  Public classMainactivityextendsActivity {2 3 Button Btn_send_receiver;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8Btn_send_receiver =(Button) Findviewbyid (r.id.btn_send_receiver);9Btn_send_receiver.setonclicklistener (NewOnclicklistener () {Ten              One @Override A              Public voidOnClick (View v) { -Intent Intent =NewIntent (); -Intent.setaction ("Com.codingblock.myreceiver.intent.action.MyReceiver"); theSendorderedbroadcast (Intent,NULL);//ordered broadcasts need to be sent using the Sendorderedbroadcast () method -             } -         }); -     } +}

The layout file has only one button, which is relatively simple and is not posted here.

By running the test, you will find that when you click the Send Message button, only myreceiver receives the message and the broadcast is interrupted. The logs are as follows:

  

Android Learning Note (12) Broadcastreceiver ordered broadcast and priority

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.