Android - Event Bus EventBus package and use

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u014619545/article/details/90512143

EventBus can easily cross-component data communications across threads, with respect to one of the four traditional BroadcastReciver assembly is more convenient to use, compact, lower coupling code.

Need to introduce the use of dependent libraries:
implementation 'org.greenrobot:eventbus:3.1.1'

There are many examples online package, here way to use annotations, annotation to define a runtime:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindEventBus {
}

Addition method used in the base class, and by determining whether to add annotations to determine whether the method invocation registration:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 若使用BindEventBus注解,则绑定EventBus
    if(this.getClass().isAnnotationPresent(BindEventBus.class)){
        EventBusUtils.register(this); //必需要先注册
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // 若使用BindEventBus注解,则解绑定EventBus
    if(this.getClass().isAnnotationPresent(BindEventBus.class)){
        EventBusUtils.unregister(this);//必须要解除注册,防止内存泄漏
    }
}

Event for encapsulation, requires a data transfer entity EventBus class object, to pass the data transmitted through the object. If not encapsulated, the data transfer to be re-created each time a class entity, to transmit data, where a good package can be fixed elsewhere multiplexed to transmitted data distinction code body, and may be acquired for determining where and processing the corresponding data.

public class Event<T> {
    private int code;
    private T data;


    public Event(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

Wrapper classes to send a message, here is more convenient package for a unified call:

public class EventBusUtils {

    /**
     * 绑定 接受者
     * @param subscriber
     */
    public static void register(Object subscriber) {
        EventBus.getDefault().register(subscriber);
    }

    /**
     * 解绑定
     * @param subscriber
     */
    public static void unregister(Object subscriber){
        EventBus.getDefault().unregister(subscriber);
    }

    /**
     * 发送消息(事件)
     * @param event
     */
    public static void sendEvent(Event event){
        EventBus.getDefault().post(event);
    }

    /**
     * 发送 粘性 事件
     *
     * 粘性事件,在注册之前便把事件发生出去,等到注册之后便会收到最近发送的粘性事件(必须匹配)
     * 注意:只会接收到最近发送的一次粘性事件,之前的会接受不到。
     * @param event
     */
    public static void sendStickyEvent(Event event){
        EventBus.getDefault().postSticky(event);
    }
}

You need to send data to be transferred with EventBusUtils // when used herein can be sent only by calling the method (ABC as carrying data entity class). as follows:

Event<ABC> event=new Event<>(0,new ABC());
EventBusUtils.sendEvent(event);

Upon receiving, at the receiver at the previously defined classes with the annotation, and then write a receiving method, a method name can freely. But be sure to add a comment on the method name. details as follows:

@BindEventBus
public class ABCActivity extends BaseActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

	//方法名可以随意,但是一定要加上@Subscribe注解,并指定线程 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(Event<ABC> event){
        // do something
    }
}

Thus, to complete the package and used. Enjoy it!

Thanks:
https://blog.csdn.net/jiashuai94/article/details/79725755
https://www.jianshu.com/p/bf5c431872bf

Guess you like

Origin blog.csdn.net/u014619545/article/details/90512143