EventBus3 封装

思路

  1. 封装一个EventBus工具类,消息类MessageEvent
  2. 在BaseActivity中选择注册及接收消息,默认不注册
  3. 子Activity继承BaseActivity,如需接收消息,则重写父类的方法,返回true
  4. 在需要发送消息的地方使用工具类发送消息

____________________________________________________________

具体步骤

1.1 封装EventBusUtil类:

public class EventBusUtil {
    public static void register(Object subscriber) {
        EventBus.getDefault().register(subscriber);
    }

    public static void unregister(Object subscriber) {
        EventBus.getDefault().unregister(subscriber);
    }

    public static void sendEvent(MessageEvent event) {
        EventBus.getDefault().post(event);
    }

    public static void sendStickyEvent(MessageEvent event) {
        EventBus.getDefault().postSticky(event);
    }
}

1.2 封装MessageEvent

public class MessageEvent {
    private int code;
    private T data;

    public MessageEvent(int code) {
        this.code = code;
    }

    public MessageEvent(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;
    }
}

其中code对应不同的消息,data为发送的内容

2. 在BaseActivity中选择注册及接收消息,默认不注册

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isRegisterEventBus()) {
            Log.d("EBBaseActivity", "register");
            EventBusUtil.register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(MessageEvent event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(MessageEvent event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(MessageEvent event) {

    }
}

3. 子Activity继承BaseActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ebfirst);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button_eb1)
    public void onViewClicked() {
        EventBusUtil.sendStickyEvent(new MessageEvent<>(MyEventCode.CODE_B, mEditText.getText().toString()));
        startActivity(new Intent(this, EBSecondActivity.class));
    }


    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(MessageEvent event) {
        switch (event.getCode()) {
            case MyEventCode.CODE_A:
                String data = (String) event.getData();
                mTextView2.setText(data);
                break;
        }
    }

    @Override
    protected void receiveStickyEvent(MessageEvent event) {
        switch (event.getCode()) {

        }
    }

你可能感兴趣的:(EventBus3 封装)