参考 http://blog.csdn.net/harvic880925/article/details/40660137,http://blog.csdn.net/harvic880925/article/details/40787203
什么是EventBus
EventBus是Android下高效的发布/订阅事件总线机制。作用是可以代替传统的Intent,Handler,Broadcast或接口函数在Fragment,Activity,Service,线程之间传递数据,执行方法。特点是代码简洁,是一种发布订阅设计模式(Publish/Subsribe),或称作观察者设计模式。
如何使用EventBus
1. 定义事件, 定义一个类,继承默认的Object即可,用于区分事件和传输数据。 本例为FirstEvent
2. 添加订阅者:EventBus.getDefault().register(this); 将所在类作为订阅者,框架会通过反射机制获取所有方法及其参数。
订阅者所在类可以定义以下一个或多个方法用以接收事件:
public void onEvent(FirstEvent msg)
public void onEventMainThread(FirstEvent msg)
public void onEventBackgroundThread(FirstEvent msg)
public void onEventAsync(FirstEvent msg)
这四种订阅函数都是使用onEvent开头的,它们的功能稍有不同,在介绍不同之前先介绍两个概念:
告知观察者事件发生时通过EventBus.post函数实现,这个过程叫做事件的发布,观察者被告知事件发生叫做事件的接收,是通过下面的订阅函数实现的。
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方 法中 不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个 在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来 的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
3. 发布者发布事件:EventBus.getDefault().post(new FirstEvent ("主线程发的消息1"));
一旦执行了此方法, 所有订阅者都会执行第二步定义的方法。
4. 取消订阅:EventBus.getDefault().unregister(this); 当订阅者不再被使用,或者被关闭时,最好进行取消订阅,不再接受事件消息。
5. 注意事项:发布者post方法参数是Object类型,也就是可以发布任何事件。订阅者接受消息时,只要定义的是第二步四个方法任意一个,并且参数和发布者发布的一致,即可被执行。发布者也可以通过第二步接收消息,订阅者也可以作为发布者发消息给自己。
示例代码
-
发送消息的类
发送消息是使用EventBus中的Post方法来实现发送的
- public class SecondActivity extends Activity {
- private Button btn_FirstEvent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
- btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- EventBus.getDefault().post(
- new FirstEvent("FirstEvent btn clicked"));
- }
- });
- }
- }
- 接收消息的类
- public class MainActivity extends Activity {
- Button btn;
- TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- EventBus.getDefault().register(this);
- btn = (Button) findViewById(R.id.btn_try);
- tv = (TextView)findViewById(R.id.tv);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(getApplicationContext(),
- SecondActivity.class);
- startActivity(intent);
- }
- });
- }
- public void onEventMainThread(FirstEvent event) {
- String msg = "onEventMainThread收到了消息:" + event.getMsg();
- Log.d("harvic", msg);
- tv.setText(msg);
- Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
- }
- @Override
- protected void onDestroy(){
- super.onDestroy();
- EventBus.getDefault().unregister(this);
- }
- }
- 消息的bean类
- public class FirstEvent {
- private String mMsg;
- public FirstEvent(String msg) {
- // TODO Auto-generated constructor stub
- mMsg = msg;
- }
- public String getMsg(){
- return mMsg;
- }
- }
- 接收消息时,是通过订阅者的方法中的参数区分是谁发送的消息。比如,在订阅者的方法里有两个接收方法: public void onEvent(FirstEvent msg){}, public void onEvent(SecondtEvent msg),发布 EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked"))时,只有public void onEvent(FirstEvent msg){}方法会执行;
- EventBus.getDefault().unregister(this),这种注册的方法是一种单例模式,也就是无论在哪个类中注册,只会使用已经存在的EventBus实例对象。当在ViewPager中使用时,当在第一个pager中使用单例的方法注册,在第二个pager中也是用单例注册。当页面由第一个pager切换到第二个pager时,第一个pager不会被销毁,因此不会走onDestroy()方法,结果就是这个EventBus没有被取消注册,第二个pager中又调用注册EventBus的方法,这是就会报一个“不能重复注册EventBus”错误。 解决方法:不用单例注册EventBus,通过new EventBus()实例化,在进行注册。