EventBus 3.0(一)
来源:互联网 发布:创维28t88ht数据 编辑:程序博客网 时间:2024/06/06 03:55
什么是EventBus?
看到这个单词你是不是像我一样第一眼翻译成“事件公共汽车”?哈哈,其实Bus在计算机中的含义是“总线”,这个单词的术语应该称之为“消息总线”,大家耳熟能详的GreenDao是由greenrobot组织开发的,没错,这个EventBus也是该组织的产品。
EventBus是针一款对Android的发布/订阅消息总线。它可以让我们很轻松的实现在Android各个组件之间传递消息,并且代码的可读性更好,耦合度更低
为什么要用EventBus?
EventBus可以代替Android传统的Intent,Handler,Broadcast或接口函数,在Fragment,Activity,Service线程之间传递数据,执行方法。
对!你没有看错,以前你是不是经常使用广播和Intent收发消息?是不是经常要写一些固定的模板代码(比如下方代码)?早就有人帮你找到了更好的办法,谁还傻瓜式的敲固定的代码呢?
//注册广播 IntentFilter intentFilter = new IntentFilter("message_broadcast"); mBroadcastReceiver = new MessageBroadcastReceiver(); registerReceiver(mBroadcastReceiver, intentFilter); //发送广播 Intent intent = new Intent(); intent.setAction("message_broadcast"); intent.putExtra("message", message); sendBroadcast(intent); //这些代码就是模板代码,核心代码就几句话,还得书写其他代码就很浪费时间
简化了组件间的通讯。
分离了事件的发送者和接受者。
在Activity、Fragment和线程中表现良好。
避免了复杂的和易错的依赖关系和生命周期问题。
使得代码更简洁,性能更好。
更快,更小(约50k的jar包)。
总结
这是一种“消息总线”,用更少的代码实现事件传递,取代Android中Handler,Intent和BroadCast而产生的三方库
瞧我发现了什么?
/** * EventBus is a central publish/subscribe event system for Android. Events are posted ({@link #post(Object)}) to the * bus, which delivers it to subscribers that have a matching handler method for the event type. To receive events, * subscribers must register themselves to the bus using {@link #register(Object)}. Once registered, subscribers * receive events until {@link #unregister(Object)} is called. Event handling methods must be annotated by * {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter * (the event). * * @author Markus Junginger, greenrobot */
关于EventBus的具体使用我放到下一讲,大家这一讲先理解下这个新的概念,文章结尾附上EventBus的Github地址,大家可以看下官方的帮助文档
EventBus 3.0 Github
- EventBus 3.0 (一) 基础
- EventBus 3.0(一)
- EventBus(一)
- EventBus 3.0的用法详解(一)
- EventBus 3.0的用法详解(一)
- 了解EventBus 3.0 (一)-订阅流程
- EventBus 3.0的用法详解(一)
- EventBus 3.0的用法详解(一)
- EventBus使用(一)
- EventBus使用(一)
- EventBus 源码解析(一)
- EventBus篇一:ThreadMode
- EventBus实例教程(一)
- EventBus(一)
- EventBus使用详解(一)
- EventBus笔记(一)
- EventBus使用(一)
- EventBus源码解析一
- JAVA源码学习(1)——Integer
- 发送有序广播
- ‘Charliecloud’ simplifies Big Data supercomputing
- 面试小贴士
- 强连通分量SCC(Tarjan)
- EventBus 3.0(一)
- spring boot @ConditionalOnxxx相关注解总结
- 欢迎使用CSDN-markdown编辑器
- 神经网络压缩(4) Learning Structured Sparsity in Deep Neural Networks
- 一个小菜鸟的发展历史
- 我对知乎前端相关问题的十问十答
- WEB ARCHITECTURE
- android 手机内存不足,数据回收
- 抽象类