compile ‘de.greenrobot:eventbus:2.4.0‘
eventBus=EventBus.builder().build();
eventBus.register(this);
this为事件接收者(消费事件、处理事件…随便怎么说吧)
注意要定义一个类,这个类可以随便定义,也可以用现有的类
class Event{
public String result="default";
}
eventBus.post(event);
可以再任意线程任意位置发送事件,但要保证eventBus
在注册和发送时为同一个对象。
在注册时的this
的类中写如下方法
- public void onEventMainThread(Event event){}
接到事件后在主线程消费事件
- public void onEvent(Event event){}
接到事件后在事件post的线程中消费事件(注意不能进行耗时操作,否则影响事件传递)
- public void onEventBackgroundThread(Event event){}
在后台线程中消费事件。如果事件的post线程为后台线程,则使用post线程,否则在新的子线程中消费事件
- public void onEventAsync(Event event){}
在新的子线程中消费事件
原文:http://blog.csdn.net/xmh19936688/article/details/51510309