Reprint Declaration Original address: http://blog.csdn.net/lsyz0021/article/details/52094855
30 minutes to learn EventBus3.0 (i) (Introduction and initialization of EventBus3.0)
30 minutes to learn EventBus3.0 (ii) (EventBus3.0 use)
After writing the last article, this article this heart does not want to write, but at least a weekly habit can not change ah! Although Eventbus is very simple to use, there are many kinds of things, such as sticky events and non-sticky events, and they all have four modes, and they also have to be tested on the UI thread and non-UI threads, so there are a lot of different kinds of nonsense to keep in view.
1, Eventbus tool class, here in order to use convenient to write a simple tool class, the following articles are used this tool class.
<span style= "FONT-SIZE:14PX;" >public class Eventbusutils {private Eventbusutils () {}/** * register Eventbus */public static void Register (Object subscriber ) {if (! Eventbus.getdefault (). isregistered (subscriber)) Eventbus.getdefault (). Register (subscriber); /** * Unregister eventbus */public static void Unregister (Object subscriber) {Eventbus.getdefault (). Unregister (subscriber);} /** * Publish subscription event */public static void Post (Object subscriber) {Eventbus.getdefault (). Post (subscriber);} /** * Publish sticky subscription events */public static void Poststicky (Object subscriber) {Eventbus.getdefault (). Poststicky (subscriber);} /** * Removes the specified sticky subscription event * @param EventType class bytecode, for example: String.class */public static <T> void Removestickyevent (class< T> eventtype) {T stickyevent = Eventbus.getdefault (). Getstickyevent (EventType); if (stickyevent! = null) { Eventbus.getdefault (). Removestickyevent ((T) stickyevent);}} /** * Remove all Sticky subscription events */public static void Removeallstickyevents () {Eventbus.getdefault (). Removeallstickyevents ();}} </span>
2. Create a subscription event
<span style= "FONT-SIZE:14PX;" >public class Messageevent {public final String message;public messageevent (String message) {this.message = message;}} </span>
3. Create subscriber patterns and types
3.1. Subscriber mode is divided into four categories:
<span style= "FONT-SIZE:14PX;" >threadmode.main regardless of which thread emitted the event, the main mode executes in the UI (main thread) of the Threads threadmode.posting The thread from which the event was published will run Threadmode.background if the thread that sends the event is the UI thread, re-creates the new child thread, and therefore cannot perform the update UI operation Threadmode.async Regardless of which thread emitted the event, Async mode creates a new child thread to execute </span>
3.2, the type of subscribers is divided into sticky events and non-sticky events
The default is non-sticky event, if it is sticky event, only need to add the note above the event
True
3.3, sticky events, add priority to the annotations = precedence series (int value)
1
3.4, the following on the various modes are explained separately
3.4.1, first Eventbus registration, registered in Activity's OnStart ()
<span style= "FONT-SIZE:14PX;" > @Overrideprotected void OnStart () {Super.onstart (); Eventbusutils.register (this);} </span>
Eventbus Cancel registration and cancel registration in activity's OnDestroy ()
<span style= "FONT-SIZE:14PX;" > @Overrideprotected void OnDestroy () {Super.ondestroy (); Eventbusutils.unregister (this);} </span>
3.4.2, sending events
<span style= "FONT-SIZE:14PX;" >eventbusutils.post (New Messageevent ("Secondactivity released Messageevent Message"));</span>
3.4.3, registering events (with priority)
Mian mode
<span style= "FONT-SIZE:14PX;" > @Subscribe (Threadmode = threadmode.main, priority = 1) public void Onmessageeventmain (Myevent.message event) { Text2.settext (EVENT.MSG); LOG.V (tag, event.msg + "MAIN id =" + thread.currentthread (). GetId ());} </span>
Posting mode
<span style= "FONT-SIZE:14PX;" > @Subscribe (Threadmode = threadmode.posting, priority = 2) public void Onmessageeventpost (Myevent.message event) {if ( Ismainthread ()) {text2.settext (event.msg); LOG.V (tag, event.msg + "POSTING id =" + thread.currentthread (). GetId ());} else {LOG.V (tag, event.msg + "POSTING id =" + thread.currentthread (). GetId ());}} </span>
Background mode
<span style= "FONT-SIZE:14PX;" > @Subscribe (Threadmode = threadmode.background, priority = 3) public void Onmessageeventbackground (myevent.message Event) {if (looper.mylooper () = = Looper.getmainlooper ()) {text2.settext (event.msg); LOG.V (tag, event.msg + "BACKGROUND id =" + thread.currentthread (). GetId ());} else {LOG.V (tag, event.msg + "BACKGROUND id =" + thread.currentthread (). GetId ());}} </span>
Async Mode
<span style= "FONT-SIZE:14PX;" > @Subscribe (Threadmode = threadmode.async, priority = 4) public void Onmessageeventasync (Myevent.message event) {// Text2.settext (event.msg);//cannot perform the update UI operation here LOGUTILS.V (tag, event.msg + "Async id =" + thread.currentthread (). GetId ());} </span>
If the sticky event is:
Send Sticky events
<span style= "FONT-SIZE:14PX;" >eventbusutils.poststicky (New Messageevent ("Secondactivity released Messageevent Message"));</span>
Subscribe to sticky messages
<span style= "FONT-SIZE:14PX;" > @Subscribe (threadmode = threadmode.main, sticky = true) public void Messageevent (Messageevent event) {tv_ Text1.settext (event.message);} </span>
For more details, see Code: Https://github.com/lsyz0021/EventBusUtils
30 minutes to learn EventBus3.0 (ii) (EventBus3.0 use) (by Star Wuge)