Android---EventBus3.0版本的使用及说明

作者: 心若冰清_ | 来源:发表于2017-11-23 17:05 被阅读135次

    EventBus3.0事件介绍


    EventBus是一个基于观察者模式,并且是针对了Android进行优化的发布/订阅的事件总线。简化了应用程序内各组件间、组件与后台线程间的通信。优点是开销小,代码更优雅,以及将发送者和接收者解耦,避免了产生大量的内部类。

    EventBus3.0应用的范围比较广,比如请求网络,当请求网络后,可以通过handler和broadcast通知UI,同样也可以使用EventBus3.0实现。Activity和Activity进行交互还好说,如果Fragment和Fragment进行交互着实令人头疼,我们会使用广播来处理,但是使用广播稍显麻烦并且效率也不高,如果传递的数据是实体类需要序列化,那么很显然成本会有点高。

    因此在涉及Activity与Fragment之间,Fragment与Fragment之间进行交互的时候,优先考虑使用EventBus3.0进行通信。关于EventBus3.0更多的介绍可以参考EventBus官方介绍:

    EventBus3.0 GitHub地址链接
    EventBus3.0官网文案


    EventBus3.0的三大要素


    讲述eventBus的工作流程,可以先看下官网给出的关系图

    官网提供的关系图

    事件的发布者Publisher通过post事件,将具体的事件通知给事件的订阅者Subscriber,然后在由订阅者对接收到的事件进行处理。


    Event:事件

    可以是任意类型的数据,比如简单的string类型的获取;

    /**
    * 简单的事件类型
    */
    public class updateTextEvent {
        private static final String TAG = "AA";
        private String msg;
    
        public updateTextEvent(String msg) {
            this.msg = msg;
            Log.i(TAG, "updateTextEvent: " + msg);
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    

    Publisher:事件发布者

    可以在任意线程任意位置发送事件,直接调用EventBus的post(Object)方法。可以自己实例化EventBus对象,但一般使用EventBus.getDefault()就好了,根据post函数参数的类型,会自动调用订阅相应类型事件的函数。

    Subsriber:事件订阅者

    在EventBus3.0之前的EventBus1.0和EventBus2.0时代消息处理的方法只能限定于onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync,他们分别代表四种线程模型。而在EventBus3.0之后,事件处理的方法可以随便取名,但是需要添加一个注解@Subscribe,并且要指定线程模型(默认为POSTING)。例:

    /**
    * 订阅的过程中,默认是在主线程中用到的
    */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void modifyBtn4(updateTextEvent msg){
        button.setText(msg.getMsg());
    }
    

    综合上述的流程及对于其中重要的三个元素的认识,可以认为在使用EventBus3.0总共分为以下几个步骤:
    前提:
    在Android studio的module的build.gradle中配置编译库连接:目前最新的版本为3.1.1

    compile 'org.greenrobot:eventbus:3.1.1'
    
    • 事件的定义

    public static class MessageEvent { 
    /* 
    Additional fields if needed 
    */ 
    }
    
    • 事件订阅者的准备

    声明并且采用注解的方式来定义订阅者的方法,默认的情况下,注解方法中的threadMode为MAIN,表示在主线程中运行,事件处理的函数名可以自行定义。下面会讲述4中线程的模式。

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /* Do something */
    };
    
    • 事件的发布者的准备

    事件发布者的工作相对比较简单,最常用的是采用post或者postSticky的方法,将new出来的事件对象发布出来,这两种发布的方法,将会在下面知识讨论中进行讲述其区别。

    EventBus.getDefault().post(new MessageEvent());
    

    那么问题来了,时间的订阅者如何才能得知有事件被发布了呢?因此在使用EventBus3.0的时候需要在事件订阅的那个activity或者fragment中进行注册,其注册代码的地方依附于activity或者fragment的生命周期,即:

    @Override
    public void onStart() {
        super.onStart();
    //在事件被订阅的界面中注册EventBus
        EventBus.getDefault().register(this);
    }
    
    @Override
    public void onStop() {
        super.onStop();
    //注销EventBus
        EventBus.getDefault().unregister(this);
    }
    

    EventBus的四种线程模型---ThreadMode


    在讲述事件订阅者的时候,采用添加注解的方法@Subscribe(threadMode = ThreadMode.MAIN)对事件的处理进行区别,查看EventBus3.0线程模型文档,有以下几种线程模型,来帮助开发者针对不同的事件,和事件消费类型进行选择。

    • ThreadMode: POSTING

    默认的线程模式,如果使用事件处理函数指定了线程模型为POSTING,那么该事件在哪个线程发布出来的,事件处理函数就会在这个线程中运行,也就是说发布事件和接收事件在同一个线程。此外,在线程模型为POSTING的事件处理函数中尽量避免执行耗时操作,因为它会阻塞事件的传递,甚至有可能会引起ANR。

    • ThreadMode: MAIN

    事件的处理会在UI线程中执行。此外也不能在处理事件中执行比较耗时的操作,否则也会引起ANR。

    • ThreadMode: BACKGROUND

    如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的线程中运行,如果事件本来就是子线程中发布出来的,那么该事件处理函数直接在发布事件的线程中执行。在此事件处理函数中禁止进行UI更新操作。

    • ThreadMode: ASYNC

    无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行,同样,此事件处理函数中禁止进行UI更新操作,因为这牵涉到UI的更新只能在 main thread中更新。

    在通过学习并了解了上面的EventBus3.0的相关知识后,可以进行一些数据的通信了。按照EventBus3.0的三大要素,分别进行操作实现数据的通信。

    • 首先添加库依赖
    //ButterKnife控件绑定依赖
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    
    //EventBus控件绑定
    compile 'org.greenrobot:eventbus:3.1.1'
    
    • 事件
    /**
     * Copyright :All rights reserved
     *
     * @Description : 更新相关的text的内容
     * @Author : Brian
     * @Date : 2017/11/23
     */
    public class updateTextEvent {
        private static final String TAG = "AA";
        private String msg;
    
        public updateTextEvent(String msg) {
            this.msg = msg;
            Log.i(TAG, "updateTextEvent: " + msg);
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
    • 事件的订阅者及事件的订阅者
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "AA";
        @BindView(R.id.button1)
        Button button1;
        @BindView(R.id.button2)
        Button button2;
        @BindView(R.id.button3)
        Button button3;
        @BindView(R.id.button4)
        Button btn4;
        @BindView(R.id.button5)
        Button button5;
       /* @BindViews({R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,R.id.button6})
        List<Button> list;*/
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        //注册ButterKnife
            ButterKnife.bind(this);
        //注册EventBus
            EventBus.getDefault().register(this);
        }
    
        //添加对于button 按钮事件的注解
        @OnClick({R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.button1:
                    Toast.makeText(this,"开始",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button2:
    //                EventBus.getDefault().postSticky(new updateTextEvent("这是一个测试"));
                    EventBus.getDefault().post(new updateTextEvent("这是一个测试"));
    //                Intent intent = new Intent(this,SecActivity.class);
    //                startActivity(intent);
    
                    break;
                case R.id.button3:
    
                    break;
                case R.id.button4:
    
                    break;
                case R.id.button5:
                    break;
            }
        }
      
        //更改button4的文本内容            
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void modifyBtn4(updateTextEvent msg){
    //        list.get(3).setText(msg.getMsg());
            btn4.setText(msg.getMsg());
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        //注销EventBus
            EventBus.getDefault().unregister(this);
        }
    }
    

    细心的读者应该也注意到了在上述MainActivity中,使用到了Jake Whaton大神的butterknife的使用。测试结果如下,可以正常的进行数据的通信。

    Activity测试通信OK

    按照上面的事件发布及订阅事件,在同一个activity中进行事件的通信是没有问题的,那如果按下上述的button2,能否将这个事件发布通知到其他activity中的订阅者呢?新建一个SecActivity,代码如下:

    public class SecActivity extends AppCompatActivity {
        @BindView(R.id.textview)
        TextView text;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sec);
    
            ButterKnife.bind(this);
    
            EventBus.getDefault().register(this);
        }
    
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void cha(updateTextEvent msg){
            text.setText(msg.getMsg());
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }
    }
    

    同时在修改MainActivity中的代码,注释掉EventBus的注册和注销代码。

    //EventBus.getDefault().register(this);
    //EventBus.getDefault().unregister(this);
    

    并且添加activity的跳转代码,当按下button2的时候,正常跳转到SecActivity,但是并没有预期的修改text的现象,查询log如下,提示并没有注册订阅者。

    11-23 07:55:43.599 6286-6286/com.example.myapplication D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
    
            --------- beginning of system
            11-23 07:55:43.652 6286-6325/com.example.myapplication D/EGL_emulation: eglMakeCurrent: 0xaa7c7240: ver 2 0 (tinfo 0xaa7e00a0)
            11-23 07:55:43.653 6286-6325/com.example.myapplication D/EGL_emulation: eglMakeCurrent: 0xaa7c7240: ver 2 0 (tinfo 0xaa7e00a0)
    

    理清思路,事件的发布和订阅是与activity的生命周期有关系的,如何解决这个问题呢?还记得上面讲述的postSticky()方法吗?

    查询这个这个文档:Sticky Events的官方文档,意译为:先发布事件,然后在订阅

    因此修改MainActivity中post代码为:

    EventBus.getDefault().postSticky(new updateTextEvent("这是一个测试"));
    

    与此同时,也需要修改SecActivity的代码:

    public class SecActivity extends AppCompatActivity {
        @BindView(R.id.textview)
        TextView text;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sec);
    
            ButterKnife.bind(this);
    
            EventBus.getDefault().register(this);
        }
    
        @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
        public void cha(updateTextEvent msg){
            text.setText(msg.getMsg());
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }
    }
    

    测试结果如下图:

    image.png

    至此,EventBus3.0的常用方法及使用的说明,开发者应该有了基本的认识了。愉快的coding吧。

    EventBus3.0的发布及广泛的应用已经有很长时间了,文中部分内容参考借鉴了其他开发者的博客内容:

    Android之EventBus1.0 和EventBus3.0的使用详解
    Android事件总线(一)EventBus3.0用法全解析
    关于EventBus3.0使用,你看这篇就够了

    非常感谢简书提供的这个知识汇聚分享的平台。

    相关文章

      网友评论

        本文标题:Android---EventBus3.0版本的使用及说明

        本文链接:https://www.haomeiwen.com/subject/xggzvxtx.html