EvenBus3.0 Usage Details, evenbus3.0 details
Description: EventBus is an Android-optimized publish/subscribe message bus that simplifies communication between components in an application and between components and background threads. For example, in the request network, the UI is notified through Handler or Broadcast when the network returns. The two Fragment must communicate with each other through Listener. These requirements can be implemented through EventBus.
Advantages:
-Simple and powerful: EventBus is a small library of very easy-to-learn APIs. However, your software architecture may benefit from decoupling components: subscribers do not know the sender when using events. -Combat test: EventBus is one of the most common Android libraries: thousands of applications use EventBus, including very popular applications. More than 1 billion of applications are installed. -High Performance: especially on Android, performance is very important. EventBus has been analyzed and optimized a lot; it may become the fastest solution of its kind. Convenient annotation-based API (without sacrificing performance): simply add the @ Subscribe annotation to your user method. Because of the build time index of annotations, EventBus does not need to perform annotation reflection when the application is running, which is very slow on Android. -Android main thread delivery: when interacting with the UI, EventBus can pass events in the main thread, regardless of how the events are published. -Background thread transfer: If your user runs the task for a long time, EventBus can also use background threads to avoid UI blocking. -Event and subscriber inheritance: In EventBus, the object-oriented example applies to the event and subscriber classes. Assume that event class A is the superclass of B. Release events of type B will also be published to users interested in type. Similarly, consider the inheritance of the subscription class. -Zero Configuration: you can immediately start using the default EventBus instance provided anywhere in the code. -Configurable: To adjust EventBus as needed, you can use the builder mode to adjust its behavior.
1. build. gradle add reference
compile 'org.greenrobot:eventbus:3.0.0'
2. Build a message class
public class MessageEven { private String message; public MessageEven(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }}3. Registration before use and cancellation after use
EventBus. getDefault (). register (this); // register
EventBus. getDefault (). unregister (this); // deregister
4. Send messages
EventBus.getDefault().post(new MessageEven("Content"));5. select four thread methods based on the actual situation to process the received message logic.
/*** Run @ param msg */@ Subscribe (threadMode = ThreadMode. MAIN) public void onMainEventBus (MessageEven even) {// Message Processing Log. e ("= Main", Thread. currentThread (). getName ();}/*** background thread * @ param msg */@ Subscribe (threadMode = ThreadMode. BACKGROUND) public void onBackgroundEventBus (MessageEven even) {Log. e ("= OnBack", Thread. currentThread (). getName ();}/*** asynchronous thread ** @ param msg */@ Subscribe (threadMode = ThreadMode. ASYNC) public void onAsyncEventBus (MessageEven even) {Log. e ("= onAsy", Thread. currentThread (). getName ();}/*** by default, which is in the same thread as the event to be sent ** @ param msg */@ Subscribe (threadMode = ThreadMode. POSTING, priority = 100) public void onPostEventBus (MessageEven even) {Log. e ("= onPost", Thread. currentThread (). getName (); EventBus. getDefault (). cancelEventDelivery (even );}