EventBus的学习与使用

来源:互联网 发布:linux服务器安全策略 编辑:程序博客网 时间:2024/05/19 23:56
EventBus 是一个 Android 事件发布/订阅框架,通过解耦发布者和订阅者简化 Android 事件传递,这里的事件可以理解为消息,本文中统一称为事件。事件传递既可用于 Android 四大组件间通讯,也可以用户异步线程和主线程间通讯等等。

传统的事件传递方式包括:Handler、BroadCastReceiver、Interface 回调,相比之下 EventBus 的优点是代码简洁,使用简单,并将事件发布和订阅充分解耦。  

事件(Event):又可称为消息,本文中统一用事件表示。其实就是一个对象,可以是网络请求返回的字符串,也可以是某个开关状态等等。事件类型(EventType)指事件所属的 Class。
事件分为一般事件和 Sticky 事件,相对于一般事件,Sticky 事件不同之处在于,当事件发布后,再有订阅者开始订阅该类型事件,依然能收到该类型事件最近一个 Sticky 事件。

订阅者(Subscriber):订阅某种事件类型的对象。当有发布者发布这类事件后,EventBus 会执行订阅者的 onEvent 函数,这个函数叫事件响应函数。订阅者通过 register 接口订阅某个事件类型,unregister 接口退订。订阅者存在优先级,优先级高的订阅者可以取消事件继续向优先级低的订阅者分发,默认所有订阅者优先级都为 0。

发布者(Publisher):发布某事件的对象,通过 post 接口发布事件。

-------------------------------------------------------------------------------------------------------------------------------------------

下面介绍EventBus的具体使用:

EventBus.getDefault().register(this); //注册订阅
EventBus.getDefault().unregister(this);//去掉订阅
EventBus.getDefault().post(msg); //发布时间
public void onEvent(String event);//响应时间
该方法名为 onEvent 则 threadMode 为ThreadMode.PostThread
该方法名为 onEventMainThread 则 threadMode 为ThreadMode.MainThread
该方法名为 onEventBackgroundThread 则 threadMode 为ThreadMode.BackgroundThread
该方法名为 onEventAsync 则 threadMode 为ThreadMode.Async
其他情况且不在忽略名单 (skipMethodVerificationForClasses) 中则抛出异常。

ThreadMode介绍:

ThreadMode共有四类:

  1. PostThread:默认的 ThreadMode,表示在执行 Post 操作的线程直接调用订阅者的事件响应方法,不论该线程是否为主线程(UI线程)。当该线程为主线程时,响应方法中不能有耗时操作,否则有卡主线程的风险。适用场景:对于是否在主线程执行无要求,但若 Post 线程为主线程,不能耗时的操作
  2. MainThread:在主线程中执行响应方法。如果发布线程就是主线程,则直接调用订阅者的事件响应方法,否则通过主线程的 Handler 发送消息在主线程中处理——调用订阅者的事件响应函数。显然,MainThread类的方法也不能有耗时操作,以避免卡主线程。适用场景:必须在主线程执行的操作
  3. BackgroundThread:在后台线程中执行响应方法。如果发布线程不是主线程,则直接调用订阅者的事件响应函数,否则启动唯一的后台线程去处理。由于后台线程是唯一的,当事件超过一个的时候,它们会被放在队列中依次执行,因此该类响应方法虽然没有PostThread类和MainThread类方法对性能敏感,但最好不要有重度耗时的操作或太频繁的轻度耗时操作,以造成其他操作等待。适用场景:操作轻微耗时且不会过于频繁,即一般的耗时操作都可以放在这里;
  4. Async:不论发布线程是否为主线程,都使用一个空闲线程来处理。和BackgroundThread不同的是,Async类的所有线程是相互独立的,因此不会出现卡线程的问题。适用场景:长耗时操作,例如网络访问
--------------------------------

测试DEMO

public class EventActivity extends BaseActivity {private EditText eventET;private Button sendBtn;private TextView receivedEventTV;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState,R.layout.activity_send_simplest_event);initView();Intent intent = new Intent(this, SecondService.class);startService(intent);}@Overridepublic void onStart() {super.onStart();// EventBus.builder().throwSubscriberException(true).installDefaultEventBus().register(this);EventBus.getDefault().register(this);}@Overridepublic void onStop() {super.onStop();}@Overrideprotected void onDestroy() {EventBus.getDefault().unregister(this);super.onDestroy();}// Receive Eventpublic void onEvent(String event) {receivedEventTV.setText(event);}// Process when EventBus call onEvent exceptionpublic void onEvent(SubscriberExceptionEvent event) {Log.e("event", "catch SubscriberExceptionEvent");}private void initView() {eventET = (EditText) findViewById(R.id.event_content);receivedEventTV = (TextView) findViewById(R.id.receive_event);sendBtn = (Button) findViewById(R.id.send_event);sendBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(eventET.getEditableText().toString());}});}}

public class SecondService extends Service{@Overridepublic void onCreate() {super.onCreate();Log.d(SecondService.class.getName(),"create");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {EventBus.getDefault().register(this);return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {EventBus.getDefault().unregister(this);super.onDestroy();}public void onEvent(String event) {Log.d(SecondService.class.getName(),event);}@Overridepublic IBinder onBind(Intent arg0) {return null;}

项目地址:https://github.com/greenrobot/EventBus

参考:http://www.codekk.com/open-source-project-analysis/detail/Android/Trinea/EventBus%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90

0 0
原创粉丝点击