EventBus(笔记)

来源:互联网 发布:怎样安装t3软件 编辑:程序博客网 时间:2024/06/05 23:44

EventBus(3.0):

是什么?

EventBus is a publish/subscribe event bus optimized for Android.
(EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。)


如何做(Android Studio)?

1. compile 'org.greenrobot:eventbus:3.0.0'

2. 注册和反注册EventBus,将它们写在Activity或Fragment的生命周期里

 @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bt_main = (Button) findViewById(R.id.bt_main);        tv_main = (TextView) findViewById(R.id.tv_main);        //注册        EventBus.getDefault().register(this);        bt_main.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this , SecondActivity.class);                startActivity(intent);            }        });    }        @Override    protected void onDestroy() {        super.onDestroy();        //取消注册        EventBus.getDefault().unregister(this);    }

3.声明订阅方法(名称可自起,3.0之后不需要固定的onEvent开头的命名,根据@Subscribe和ThreadMode来判断):

要加@Subscribe注解和模式说明

 @Subscribe (threadMode = ThreadMode.MAIN)    public void helloMain(FirstEvent firstEvent){            }

4.post

 EventBus.getDefault().post(event);


ThreadMode模式说明(共四种):

1.ThreadMode.POSTING(原onEvent)

这是个默认的模式,订阅者将会在同一个线程里被声明。由于它避免线程切换完全,因此建议做些简单的并且在短时间能完成的任务。

" This ThreadMode implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers using this mode should return quickly to avoid blocking the posting thread, which may be the main thread. "

@Subscribe(threadMode = ThreadMode.POSTING) public void onMessage(MessageEvent event) {    log(event.message);}


2.ThreadMode.MAIN(原onEventMainThread)

订阅者将会在UI主线程里被声明,并且在该线程里应该做返回快速地操作,避免主线程阻塞。

" If the posting thread is the main thread, event handler methods will be called directly (synchronously like described for ThreadMode.POSTING). Event handlers using this mode must return quickly to avoid blocking the main thread.  "

@Subscribe(threadMode = ThreadMode.MAIN)public void onMessage(MessageEvent event) {    textField.setText(event.message);}


3.ThreadMode.BACKGROUND(原onEvnetBackground)

如果发布的线程不是主线程,则它将会被直接声明为跟原来一样的线程;如果发布的线程为主线程,则EventBus将会通过一个独立的后台线程给该线程,并按照顺序来分发它的所有事件。

"If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread."

@Subscribe(threadMode = ThreadMode.BACKGROUND)public void onMessage(MessageEvent event){    saveToDisk(event.message);}


4.ThreadMode.ASYNC(原onEventAsync)

该模式主要是用于一些需要耗时地线程操作,像网络请求,它一直都是独立于主线程和posting线程。要避免同时触发大量的长时间的处理方法来限制并发线程。EventBus会使用一个线程池来高效地复用那些完成了异步事件处理程序的通知线程。

" This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications. "

@Subscribe(threadMode = ThreadMode.ASYNC)public void onMessage(MessageEvent event){    backend.send(event.message);}

附小点:

原以为当一个EventBus post一个信息出来后只有一个会被接收到,但在多个fragment里的时候却发现会被多个同时收到,结果导致在做例如支付的时候发起了多个支付,所以这个还是要注意一下。

要注意EventBus的生命周期的调用情况,即该EventBus所处的Activity还没有被创建时,它是不会收到的;当然EventBus在所处的Activity的生命周期里被反注册掉,也同样收不到。所以当你在OneActivity想post给TwoActivity的时候,你需要先测试下它是否真的能起作用。



参考和扩展:

Android解耦库EventBus的使用和源码分析

EventBus: Events for Android (推荐)

EventBus使用详解(一)——初步使用EventBus

android 初识EventBus (推荐)

Android 中 EventBus 的使用(2):缓存事件

Why you should avoid using an event bus   为什么你应该停止使用EventBus(翻译)

0 0
原创粉丝点击