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