1.添加依赖
compile 'org.greenrobot:eventbus:3.1.1'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
在 android>>defaultConfig 节点下面增加
javaCompileOptions {
annotationProcessorOptions {
arguments = [ eventBusIndex : 'com.bwie.butterknife.MyEventBusIndex' ]
}
}
2.
a.在需要接受消息的页面注册EventBUs
EventBus.getDefault().register(this);
b.
/**
* 事件响应方法
* 接收消息
* @param event
*/
// @Subscribe(threadMode = ThreadMode.MAIN)
@Subscribe(sticky = true)
public void onEvent(AnyEventType event) {
String msg = event.getMessgae();
bt_text.setText(msg);
Log.i("bt_text=",bt_text.getText().toString());
}
AnyEventType为自定义的bean对象
c.取消注册
EventBus.getDefault().unregister(this);
3.发送消息的页面
EventBus.getDefault().post(new AnyEventType("Just do it"));
运行结果:2中页面接收到了消息 "Just do it"
参考链接:
https://www.jianshu.com/p/a040955194fc
https://blog.csdn.net/wanleisuper/article/details/78679929