一、概述
EventBus是一个Android优化的发布/订阅事件总线,简化了应用程序内各组件间、组件与后台线程间的通信。
二、简单使用
1、创建一个Event,可以是空类。比如:
public class TestEvent{
}
</pre><div>2、在消息发送界面调用方法:</div><pre class="html" name="code">EventBus.getDefault().post(new TestEvent());
post方法中所传参数为你创建的Event。
3、在接收消息界面注册(一般在onCreate方法中):
EventBus.getDefault().register(this);
4、解除注册(一般在onDestroy()方法中)
EventBus.getDefault().unregister(this);
5、在接收消界面中重写方法:
@Subscribe
public void onRefureshEvent(TestEvent event) {
}
EventBus的重写的接收方法有onEvent,onEventMainThread,onEventBackgroundThread,onEventAsync这四个,都必须加上@Subscribe注解,但是这里的方法名字其实可以自己定义,只要有@Subscribe注解就行。