作用 代替Handler、接口函数,Broadcast、Intent进行数据传输。
使用方法 github下载源码框架,进行
接受者类里面需要添加的以下方法:
MsgEvent2 类可以是继承于Object的类,用于存储需要传递的数据(事件2),区别不同时间用instanceof 来区别
“onEvent(MsgEvent2 msg)
//异步调用,与post线程不同,新开一个线程
onEventAsync(MsgEvent2 msg)
//如果post在主线程,则新开子线程,如果post在子线程,则在该线程中调用
onEventBackgroundThread(MsgEvent2 msg)
//主线程中被调用
onEventMainThread(MsgEvent2 msg) ``
接受者类进行动态的注册观察和取消,一般在生命周期方法中进行
EventBus.getDefault().register(this);注册
EventBus.getDefault().unregister(this);取消注册
发送者:
EventBus.getDefault().post(new MsgEvent1(“主线程发的消息1”));发送数据