30分钟学会EventBus3 0详解(一)(引入和初始化EventBus3 0)(by星空武哥)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.****.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
转载声明原创地址:http://blog.****.net/lsyz0021/article/details/51985307
30分钟学会EventBus3.0详解(一)(引入和初始化EventBus3.0)
30分钟学会EventBus3.0详解(二)(EventBus3.0的详细使用)
前言
EventBus是greenrobot出品的非常优秀的基于发布、订阅模式的一套框架,很早就想写篇文章介绍他的文章,可是一直也没时间写,今天就写篇文章也算总结一下吧。
首先说说EventBus的优点:它是一个基于观察者模式的事件发布/订阅框架,开发者可以通过极少的代码去实现多个模块之间的通信,而不需要以层层传递接口的形式去单独构建通信桥梁。从而降低因多重回调导致的模块间强耦合,同时避免产生大量内部类。它拥有使用方便,性能高,接入成本低和支持多线程的优点,实乃模块解耦、代码重构必备良药。
它支持Activities, Fragments, Threads, Services之间的数据传递,这是他的官方原理图。
EventBus3.0和之前的EventBus2.4使用上是有区别的,由于3.0使用了注解的方式,并且增加了“SubscriberInfoIndex”要比2.0的效率高出了很多,所以今天我们讲解EventBus3.0的使用。
2017年3月13日文章修改(gradle2.2.0后的代码生成)
由于gradle2.2.0修改了注解的生成方式,所以在eventbus3.0生成“MyEventBusIndex”也不相同。
在gradle2.2.0之前使用:android-apt
在gradle2.2.0之后使用:annotationProcessor
引入EventBus3.0
gradle2.2.0之前引入EventBus3.0
1、在app的build.gradle中的 dependencies 中添加的 EventBus3.0的依赖
compile 'org.greenrobot:eventbus:3.0.0'
2、如果你想开启加速模式,你还需要配置eventbus-annotation-processor
(1)在dependencies 中添加
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
(2)在dependencies的下方添加
apt { arguments { eventBusIndex "com.bandeng.MyEventBusIndex" }}
(3)配置plugin
apply plugin: 'com.neenbedankt.android-apt'
app的build.gradle中的配置图
(4)最后这里在project build.gradle配置
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
gradle2.2.0之后引入EventBus3.0
如果你使用的gradle2.2.0及更高版本,那么就需要下面的方式引入Eventbus了,在build.gradle中配置如下
android { defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments = [ eventBusIndex : 'com.bandeng.MyEventBusIndex' ] } } }} dependencies { compile 'org.greenrobot:eventbus:3.0.0' annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'}
看图配置生成加速的MyEventBusIndex类
设置完成后要记得rebuild一下
rebuild之后,我们就可以做在build目录下看到MyEventBusIndex这个类了(如果还没有生成的话,可以先将app运行到手机后再rebuild试试)
初始EventBus
EventBus有个获取单利的方法EventBus.getDefault(),这样就可以获取EventBus对象了
EventBus.getDefault()
但是,如果我们还想要用加速模式,那么就要先现在Application的onCreate()方法中调用下面的方法/** * 作者: lcw on 2016/7/6. * 博客: http://blog.****.net/lsyz0021/ */public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // 启用EventBus3.0加速功能 EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus(); }}
然后在清单文件的Application节点下配置name为
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:name=".MyApplication" android:theme="@style/AppTheme"> <activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".activity.SecondActivity"/></application>
这样我们再调用EventBus.getDefault()就开启了加速模式了
EventBus.getDefault();
我在这里写了一个EventBus工具类,方便大家使用,可以参考一下。https://github.com/lsyz0021/EventBusUtils
30分钟学会EventBus3.0详解(一)(引入和初始化EventBus3.0)
30分钟学会EventBus3.0详解(二)(EventBus3.0的详细使用)