Abstract: This article mainly wrote handler, Broadcastreceiver, eventbus the use of three kinds of message delivery mechanism, it is strongly recommended to use the last one, The reasons are as follows: 1. Fully decoupled, the sender and the recipient are almost unrelated, and deleting one of them has no effect on the other (this is handler). 2. Easy to pass the parameters, while supporting a sender to send multiple messages, one recipient accepts multiple messages.
1.Handler:
(1). Send:
public Handler parenthandler;//This handle assignment at destination //Send handle notification
message msg = new Message ();
msg.what = praise; msg.obj = comment_id; parenthandler.handlemessage (msg);
(2). Accept:
//Accept handle notifications Handler MessageHandler = new Handler () { public void Handlemessage (Message msg) { if (msg.what = = b1_commentadapter.praise) { &N Bsp comment_id = (String) msg.obj; & nbsp Specific implementation methods praisecomment (comment_id); &NB Sp } } };
Adapter.parenthandler = messagehandler;//at destination handle Assignment
2.BroadcastReceiver:
(1). Send://Send receiverNoticeprivate void Initreceiver () {
Intent Intent = new Intent () intent.setaction ("Action.refreshmsgnumber"); Mcontext.sendbroadcast (Intent)
;
}
(2). Receive:Register private void Initreceiver () {Intentfilter intentfilter = new Intentfilter (); Intentfilter.addaction ("Action.refreshmsgnumber"); Getactivity (). Registerreceiver (Mrefreshbroadcastreceiver, Intentfilter);
}
//anti-registration
@Override public void OnDestroy () {Super.ondestroy (); Getactivity (). Unregisterreceiver (Mrefreshbroadcastreceiver); }
//receive receiver notificationsPrivate Broadcastreceiver Mrefreshbroadcastreceiver = new Broadcastreceiver () {
@Override public void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals ("Action.refreshmsgnumber")) {
//Concrete Implementation Method
Refreshmsgnumber (); } } };
3.EventBus:
"Resources"1. Need to download Eventbus.jar,I put on the 360 cloud disk:Http://yunpan.cn/cwDjxfGuMUKpDAccess Password 6b1b
"method"
(1). Send: //Send Eventbus event
Eventbus.getdefault (). Post (new Mytoolnumberevent (mytoolnumberevent.settoolnumber,weiyuedu));
(2). Accept:
AcceptEventbusEventsHandler MessageHandler = new Handler () {public void Handlemessage (Message msg) {if (Msg.what = = b1_com mentadapter.praise) {comment_id = (String) msg.obj;//Concrete Implementation MethodPraisecomment (comment_id); } } };
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Register Eventbus Eventbus.getdefault (). Register (this); } @Override public void Ondestroyview () {Super.ondestroyview (); Anti-registration Eventbus Eventbus.getdefault (). Unregister (this); }
/** * Accepting custom events and distributing processingOneventmainthread also grams* Eventbus is based on mytoolnumberevent and other class names to determine the listening events, and then can be based on refreshnumber and other parameters to determine the specific function * @param event */public void Oneventma Inthread (mytoolnumberevent event) {switch (event.getaction ()) {case Mytoolnumberevent.settoolnumber: Showmsgnumber (Event.getnumber ()); Break } }
(3). Custom event classes:
Custom events: Toolbar Digital Processing public class Mytoolnumberevent implements serializable{ public static final int Settoolnumber =0;//Settings Toolbar number public static final int refreshnumber =1;//decrease toolbar number int action; Event name INT number;//data public mytoolnumberevent (int action,int number) { &NB Sp This.action = action; This.number = number; } public int getaction () { return action; } public void setaction (int action) { & nbsp This.action = action; } public int getnumber () { return number ; } public void Setnumber (int number) { This.number = number; & nbsp }}
"Other"
In addition to the oneventmainthread, and onevent , oneventbackgroundthread, oneventbusasync, the exact difference is as follows:
A, onEvent it corresponds to the postthread in ThreadModel, this is also the default type, when this type is used, the callback function and the function that initiates the event are executed in the same thread
B, Oneventmainthread, when this type is used, the callback function executes in the main thread, which is useful in Android because it is forbidden to modify the UI in a child thread in Android
C, Oneventbackgroundthread, when this type is used, if the event initiation function executes in the main thread, then the callback function starts a child thread, and if the event initiation function executes on the child thread, then the callback function executes on that child thread.
D, Oneventbusasync, when this type is used, no matter where the event initiation function executes, another thread executes the callback.
References:
1.Android in the Open Source Library Eventbus use detailed
the use and source code analysis of 2.Android decoupling library Eventbus
From for notes (Wiz)
Collation: 3 Ways to compare messaging mechanisms: Handler, Broadcastreceiver, Eventbus