Android-Send broadcast, ordered unordered

Source: Internet
Author: User

General broadcast

    • Pros and Cons: Contrary to the pros and cons of ordered broadcasts!
    • How to send a broadcast: Sendbroadcast ()

Orderly broadcast

    • Advantages and Disadvantages

Pros: 1, priority receiver can process the data and pass it to the next receiver, depending on the priority

2, the transmission of broadcasting can be terminated by abortbroadcast

Cons: Low efficiency

    • How to send a broadcast: Sendorderedbroadcast ()
    • Receivers receiving broadcast first receive the processing result in broadcast via the Setresultextras (bundle) method, and the next receiver passes the bundle Bundle=getresultextras (true) method to get the data from the previous receiver.

Engineering

Mainactivity.java is a program to live import.

Mybroadcastreceiver.java and so on are broadcast receivers, but the level is not the same.

Finalrecevier.java is an orderly broadcast, even if the broadcast is intercepted, this can still receive the broadcast.

<receiverAndroid:name=". Mybroadcastreceiver ">            <Intent-filterandroid:priority= "+">                <ActionAndroid:name= "Com.yydcdut.nima"/>            </Intent-filter>        </receiver>        <receiverAndroid:name=". MyBroadcastReceiver2 ">            <Intent-filterandroid:priority= "+">                <ActionAndroid:name= "Com.yydcdut.nima"/>            </Intent-filter>        </receiver>        <receiverAndroid:name=". MyBroadcastReceiver3 ">            <Intent-filterandroid:priority= "Ten">                <ActionAndroid:name= "Com.yydcdut.nima"/>            </Intent-filter>        </receiver>        <receiverAndroid:name=". Finalrecevier ">            <Intent-filter>                <ActionAndroid:name= "Com.yydcdut.nima"/>            </Intent-filter>        </receiver>

In Mybroadcastreceiver.java, the code is the same:

 Public class extends broadcastreceiver {    @Override    publicvoid  onreceive (context context, Intent Intent) {        System.out.println ("MyBroadcastReceiver1111111 received broadcast");        " Mybroadcastreceiver ", Toast.length_short). Show ();            }}

In the Mainactivity.java:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {Intent Intent=NewIntent (); Intent.setaction ("Com.yydcdut.nima"); //Shuffle broadcastSendbroadcast (Intent); //Orderly Broadcast//sendorderedbroadcast (intent, NULL); //sendorderedbroadcast (Intent, NULL, New Finalrecevier (), NULL, 0, NULL, NULL);    }}

Join in a class that is received by a broadcast

Abortbroadcast ();

It was to intercept the radio, but Finalrecevier still received it, no matter how the radio was intercepted.

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Broadcast send. zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.