EventBus在项目中的应用

参考 http://blog.csdn.net/harvic880925/article/details/40660137http://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方法来实现发送的
    1. public class SecondActivity extends Activity {  
    2.     private Button btn_FirstEvent;  
    3.   
    4.     @Override  
    5.     protected void onCreate(Bundle savedInstanceState) {  
    6.         super.onCreate(savedInstanceState);  
    7.         setContentView(R.layout.activity_second);  
    8.         btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);  
    9.   
    10.         btn_FirstEvent.setOnClickListener(new View.OnClickListener() {  
    11.   
    12.             @Override  
    13.             public void onClick(View v) {  
    14.                 // TODO Auto-generated method stub  
    15.                 EventBus.getDefault().post(  
    16.                         new FirstEvent("FirstEvent btn clicked"));  
    17.             }  
    18.         });  
    19.     }  
    20. }  


  •  接收消息的类
    1. public class MainActivity extends Activity {  
    2.   
    3.     Button btn;  
    4.     TextView tv;  
    5.   
    6.     @Override  
    7.     protected void onCreate(Bundle savedInstanceState) {  
    8.         super.onCreate(savedInstanceState);  
    9.         setContentView(R.layout.activity_main);  
    10.   
    11.         EventBus.getDefault().register(this);  
    12.   
    13.         btn = (Button) findViewById(R.id.btn_try);  
    14.         tv = (TextView)findViewById(R.id.tv);  
    15.   
    16.         btn.setOnClickListener(new View.OnClickListener() {  
    17.   
    18.             @Override  
    19.             public void onClick(View v) {  
    20.                 // TODO Auto-generated method stub  
    21.                 Intent intent = new Intent(getApplicationContext(),  
    22.                         SecondActivity.class);  
    23.                 startActivity(intent);  
    24.             }  
    25.         });  
    26.     }  
    27.   
    28.     public void onEventMainThread(FirstEvent event) {  
    29.   
    30.         String msg = "onEventMainThread收到了消息:" + event.getMsg();  
    31.         Log.d("harvic", msg);  
    32.         tv.setText(msg);  
    33.         Toast.makeText(this, msg, Toast.LENGTH_LONG).show();  
    34.     }  
    35.   
    36.     @Override  
    37.     protected void onDestroy(){  
    38.         super.onDestroy();  
    39.         EventBus.getDefault().unregister(this);  
    40.     }  
    41. }  


  • 消息的bean类
    1. public class FirstEvent {  
    2.   
    3.     private String mMsg;  
    4.     public FirstEvent(String msg) {  
    5.         // TODO Auto-generated constructor stub  
    6.         mMsg = msg;  
    7.     }  
    8.     public String getMsg(){  
    9.         return mMsg;  
    10.     }  
    11. }  


    使用心得
  • 接收消息时,是通过订阅者的方法中的参数区分是谁发送的消息。比如,在订阅者的方法里有两个接收方法: 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()实例化,在进行注册。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值