【android】消息传递利器EventBus的使用
来源:互联网 发布:筹码分布软件 编辑:程序博客网 时间:2024/05/23 12:51
EventBus是一个事件发布和订阅的框架。可以替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息。优点是开销小,简化代码。
源码:https://github.com/greenrobot/EventBus
在Android studio中的集成方法如下:
第一步:
在整个项目的build.gradle中的buildscript找到dependencies,然后添加
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
第二步:
在app的build.gradle中找到dependencies,然后添加
compile 'org.greenrobot:eventbus:3.0.0'apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'并在最外层添加
apt { arguments { eventBusIndex "com.android.application.MyEventBusIndex" }}注意:此处的MyEventBusIndex类是自动生成的,帮助我们提升EventBus的性能,不需要我们手动去创建
第三步:
在application中进行初始化
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
第四步:
在取消接收消息的地方注册,比如Activity的OnCreate方法
EventBus.getDefault().register(this);
并在关闭的时候取消注册
EventBus.getDefault().unregister(this);
最后,我们只需要在需要发送消息的地方调用如下方法就可以了
EventBus.getDefault().post("EventBus测试");
0 0
- 【android】消息传递利器EventBus的使用
- Android消息传递利器EventBus
- Android:EventBus的使用 从此消息传递变得简单
- Android消息传递之EventBus 3.0使用
- Android EventBus 传递消息
- Android EventBus 传递消息
- EventBus的简单使用,消息传递
- 26.使用EventBus传递消息
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用与案例
- (转)Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解(转载)
- Android消息传递之EventBus 3.0使用详解
- Android消息传递之EventBus 3.0使用详解
- EventBus 的使用,发布,订阅消息的传递。第三方开源库
- android 消息传递机制EventBus的深入探究
- 百度地图 Android SDK - 坐标转换方法
- unity3d 动画重定向 动力学骨骼插件DynamicBone
- ubuntu16.04 64bit编译android5.1错误
- UIView的layoutSubviews和drawRect方法何时调用
- vagrant 1.下载部署linux环境
- 【android】消息传递利器EventBus的使用
- spring hadoop fsshell
- 【Core Java Volume 2】反射---打印类的全部信息
- Java语言编程的基本结构--笔记
- spring data mongo 打印NoSql语句
- 第一章1.2 算法的本质,5个特性与要求
- 堆和栈的区别
- 爬虫学习,第一节课 Your First Web Scraper
- 光电测距:距离改正