Android-sendorderedboradcast can do that, too.

Source: Internet
Author: User

As one of the four components of Android, broadcast (broadcast) is divided into regular broadcasts and orderly broadcasts. But there are several ways to send a broadcast, as follows:

Sendbroadcast (Intent Intent)
Sendbroadcast (Intent Intent, String receiverpermission)
Sendorderedbroadcast (Intent Intent, String receiverpermission)
Sendorderedbroadcast (Intent Intent,                                          String receiverpermission,                                          broadcastreceiver resultreceiver,                                          Handler Scheduler,                                          int Initialcode,                                          String Initialdata,                                          Bundle Initialextras)
Sendstickybroadcast (Intent Intent)
Previously with Sendbroadcast,sendorderedbroadcast simple send broadcast, then receive, then processing and so on. Then there was a chance to see another overloaded form of Sendorderedbroadcast (the fourth method above), hoping to see how this function is used. And not to analyze the source, feel that their current ability is not enough. But give a demo.

We know that there are two ways to start activity, startactivity and Startactivityforresult, and the second at the end (call Finish () function) will pass the parameter and return the value to his parentactivity when the activity is started, and some processing can be done when the value of the callback is received. So the broadcast is also spread, can I send a broadcast, the receiving party received, the processing of some of the data back to the sender, the sender to do the processing? Actually, but the principle is not the same as the activity, but it can be said that there are similar effects, attention, just look at the effect.

First, this is an orderly broadcast, and receiver can re-assign values and continue to propagate only through broadcasts sent by Sendorderedbroadcast.

How to re-assign a value:Setresultcode/setresultdata/setresultextras

Second, use the following method:

Sendorderedbroadcast (Intent Intent,                                          String receiverpermission,                                          broadcastreceiver resultreceiver,                                          Handler Scheduler,                                          int Initialcode,                                          String Initialdata,                                          Bundle Initialextras)
Check out the official API explanation:

Version of this allows you to receive data back from the sendBroadcast(Intent) broadcast. This was accomplished by supplying your own broadcastreceiver when calling, which would be treated as a final receiver at th E End of the broadcast-its method is called with the BroadcastReceiver.onReceive(android.content.Context, android.content.Intent) result values collected from the other receivers. If you use resultReceiver A with this method and then the broadcast'll be serialized in the same-the-as calling sendOrderedBroadcast(Intent, String) .

Parameters:

intent-The Intent to broadcast; All receivers matching this Intent would receive the broadcast.

receiverPermission-String naming a permissions that a receiver must on order to receive your broadcast. If NULL, no permission is required.

resultReceiver-Your own broadcastreceiver to treat as the final receiver of the broadcast.

scheduler-A custom Handler with which to schedule the resultreceiver callback; If NULL it is scheduled in the Context ' s main thread.

initialCode-An initial value for the result code. Often ACTIVITY.RESULT_OK.

initialData-An initial value for the result data. Often NULL.

initialExtras-An initial value for the result extras. Often NULL.

Originally did not want to see this, after all, English is not good, but the internet did not find the explanation about this method. I'll simply take a look at the official documentation, which, in summary, means that the broadcast allows you to receive the returned data from the broadcast, and your own broadcast receiver will receive the message as a final receiver, if you use the method and pass it into the Resultreceiver, This receiver is the final processing receiver, which is equivalent to calling Sendorederedbroadcast (intent,string).

As for the parameters it is good to understand, intent-the broadcast to be sent; receiverpermission-the permission to send the broadcast, if NULL, that is not considered, any qualified receiver can receive; resultreceiver-the receiver that eventually processes the data, It is defined by itself; Scheduler-a custom handler that handles the Resultreceiver callback (which is actually the thread that runs the receiver), if NULL, the default in the main thread, and the next three is not important, usually once:-1, Null,null. (ACTIVITY.RESULT_OK-1)

The code is now available:

First create a broadcast class, inherit Broadcastreceiver, implement the OnReceive method, as follows:

public class Testreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {//To Do auto-generated method STUBLOG.E ("I_broadcast", Thread.CurrentThread (). GetName ()); String s;if ((s = Intent.getstringextra ("main"))!=null) {log.e ("I_broadcast", s);} ELSE{LOG.E ("I_broadcast", "null");} Bundle bundle = new bundle (); Bundle.putstring ("Test", "Testreceiver"); Setresultextras (bundle);}}


Also declare a class to inherit Broadcastreceiver in Mainactivtiy, as follows:

Class Myreceiver extends broadcastreceiver{@Overridepublic void onreceive (context context, Intent Intent) {//TODO auto-g Enerated method Stubdebuglog ("Current thread:" +thread.currentthread (). GetName ()); Bundle bundle = Getresultextras (true); String string = bundle.getstring ("test");D Ebuglog ("string:" +string);}

Register a broadcast receiver:

Testreceiver testreceiver;testreceiver = new Testreceiver (); intentfilter filter = new Intentfilter (); Filter.addaction ( "Com.fleur.broadcast"); Registerreceiver (testreceiver, filter);


Send a broadcast:

Myreceiver myreceiver = new Myreceiver () Intent Intent = new Intent ("Com.fleur.broadcast"); Intent.putextra ("Main", " This was from mainactivity "); Sendorderedbroadcast (intent, NULL, myreceiver, NULL,-1, NULL, NULL);


Use the receiver class we declared in Mainactivity as the resultreceiver.


Now run the program print log as follows:


You can see that the data is passed back to mainactivity, and Myreceiver is running in main thread.

If I don't want Myreceiver to run in the main thread, you can open up a new thread as follows:

Handlerthread Mthread; Handler mhandler;mthread = new Handlerthread ("Mythread"); Mthread.start (); mhandler = new Handler (Mthread.getlooper ());

The Mhandler is then passed in:

Sendorderedbroadcast (Intent, NULL, Myreceiver, Mhandler,-1, NULL, NULL);

The results of this run are as follows:

Orderly broadcast natural and orderly transmission, can be passed to many broadcast receivers in turn, finally passed to our incoming broadcast receiver, to do the final processing. Does the effect look like switching activity?




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-sendorderedboradcast can do that, too.

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.