根build添加依赖
implementation 'org.greenrobot:eventbus:3.1.1'
创建ActivitySupport继承AppCompatActivity
并在
onCreate中添加EventBus.getDefault().register(this);
onDestroy中添加EventBus.getDefault().unregister(this);
新建的活动应该继承ActivitySupport
接受事件:
添加标签@Subscribe 实现事件接受处理函数 例:
@Subscribe
public void onEventMainThread(Message msg) {
switch (MsgType.values()[msg.what]) {
case TEST:
Log.i(getClass().getSimpleName(), "TEST");
break;
default:
break;
}
}
其中MsgType为枚举 根据需求添加
public enum MsgType { TEST, }
发送事件:
Bundle bundle = new Bundle();
bundle.putString("account", "0000");
bundle.putString("password","1111");
EBUtil.postMsgWithBundle(MsgType.TEST.ordinal(), bundle);