EventBus Kazuyuki basic

Common event delivery mode

Here Insert Picture Description

What is EventBus

EventBus is a publish/subscribe event bus for Android and Java.

EventBus usage scenarios

between a. simplifying components, communication components and background thread.
b. Decoupling message sender and receiver

Official Chart

Here Insert Picture Description

github address

EventBus Portal

implementation 'org.greenrobot:eventbus:3.1.1'

Use three steps

1) define an event

public class MessageEvent {
    public String text;
}

2) Subscribe to events

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

3) release event

MessageEvent msg = new MessageEvent();
EventBus.getDefault().post(msg);

Use annotation processing EventBus

The use of the above-described embodiment, implemented using the reflection obtaining subscription traversal method, the proper use EventBus, annotation process can be used to avoid reflection traversal.
1) Configure gradle support annotation processor

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
            }
        }
    }
}
 
dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

2) Subscribe to events and build project

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

The processor will automatically generate annotation class SubscriberInfoIndex


/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("handleMessageEvent", com.canjun.eventbus1.msg.MessageEvent.class,
                    ThreadMode.MAIN),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

3) using the generated class

	
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
如果要使用单例,则:
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
EventBus eventBus = EventBus.getDefault();

event

Viscous event

Scene:
When the sender sends an event of a type of event, and the method subscribe to future expectations of the type of event, we need to address this current event. In this case, the sender needs to send a tacky event, and requires subscribers to have the claims processing function can accept viscous event

 //发送方
 EventBus.getDefault().postSticky(msg);
 //接收方
 @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
 public void handleMessageEvent(MessageEvent msg){
     Toast.makeText(MainActivity.this,msg.text,0).show();
 }
Priority event

Priority event, the method determines the order of the subscription process. The higher the value of the priority, the priority. The default value is 0

@Subscribe(threadMode = ThreadMode.MAIN,priority = 1)
public void handleMessageEvent(MessageEvent msg){
     Toast.makeText(MainActivity.this,msg.text,0).show();
 }
Published 98 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/dirksmaller/article/details/104003927