compile 'org.greenrobot:eventbus:3.0.0'
官方文档:http://greenrobot.org/eventbus/documentation/how-to-get-started/
要发送的类:
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
在要接收消息的类(任何类 因为参数是Object)注册:在不需要的时候解除注册
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
接收时 :方法名任意 加这个就OK @Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
public void zoneEvent(FirstEvent event) {/* Do something */
String msg = "onEvent收到了消息:" + event.getMsg();
Logger.d(msg);
textView5.setText(event.getMsg());
}
发送时:
EventBus.getDefault().post(new FirstEvent("Hello eventBus!"));
ThreadMode: POSTING(既不切换事件):该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
@Subscribe(threadMode = ThreadMode.POSTING) // ThreadMode is optional here
public void onMessage(MessageEvent event) {
log(event.message);
}
ThreadMode: MAIN(UI线程):不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
// Called in Android UI's main thread
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
textField.setText(event.message);
}
ThreadMode: BACKGROUND(非UI线程):如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
// Called in the background thread
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessage(MessageEvent event){
saveToDisk(event.message);
}
ThreadMode: ASYNC(子线程):使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
// Called in a separate thread
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onMessage(MessageEvent event){
backend.send(event.message);
}
介绍:
EventBus allows you to post events that are “sticky” and by that EventBus understands events that “stick to the eventbus” for future access.
If you post a normal event when there are no subscribers registered at the moment of sending, this event will be discarded.
You can post a sticky event though, even if there are no subscribers to receive that at the moment, and it won’t be discarded (unless there is another sticky event posted in the future). When a subscriber registers with registerSticky the delivery of the last sticky event is also triggered.
来自:http://stackoverflow.com/questions/28051317/eventbus-register-and-registersticky-method
activity 发送这个 的时候 还没有注册 但是他把此类的最后一个对象存
EventBus.getDefault().postSticky(new FirstEvent("Hello eventBus Sticky!"));
fgment的取出 因为sticky=true所以 他从以前存的地方取出来东西
注意: stick=true 就是现在用post发的东西也可以接受的:
EventBus.getDefault().register(this);
@Subscribe(sticky = true)
public void onEvent(FirstEvent event) {
// UI updates must run on MainThread
String msg = "sticky onEvent收到了消息:" + event.getMsg();
Logger.d(msg);
}
清除缓存
EventBus.getDefault().clearCaches();
removeAllStickyEvents
removeStickyEvent(Class eventType)
removeStickyEvent(Object event)
http://blog.csdn.net/harvic880925/article/details/40660137
http://greenrobot.org/eventbus/documentation/how-to-get-started/