EventBus的使用初试
来源:互联网 发布:java api doc 编辑:程序博客网 时间:2024/05/19 13:26
当一个Android应用功能越来越多的时候,保证应用的各个部分之间高效的通信将变得越来越困难。如何优雅地解决这个问题?这时候,就需要使用到EventBus。
EventBus是GreenRobot出品的Android系统的一个Event Bus类库,使用起来和之前我们所介绍的Square的Otto差不多,都是用来简化应用组件之间的通信。
1、下载EventBus的类库
https://github.com/greenrobot/EventBus
2、基本使用
- Implement any number of event handling methods in the subscriber:接受发送的消息
public void onEvent(AnyEventType event) {}
- Register subscribers:注册
eventBus.register(this);
- Post events to the bus:发送消息
eventBus.post(event);
- Unregister subscriber:解除注册
eventBus.unregister(this);
@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);EventBus.getDefault().register(this);}@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);}public void onEvent(PhotoSelectionRemovedEvent event) {// refreshSelectedPhotosTitle();// refreshUploadActionBarView();Log.i("LogUtil", "LogUtil....onEvent"); }
2、在其他的组件中,你需要通知注册了的Activity时,只需要EventBus.getDefault().post(new PhotoSelectionRemovedEvent());上面的Activity中onEvent方法中就可以接受到消息了,去处理一些事情
- EventBus的使用初试
- EventBus 《二》 Android EventBus的简单使用
- EventBus的使用
- EventBus的使用
- EventBus的简单使用
- EventBus的使用
- eventbus的使用
- EventBus的初步使用:
- EventBus的使用
- EventBus的简单使用
- EventBus的使用总结
- EventBus的使用
- EventBus框架的使用
- EventBus的简单使用
- Eventbus的基本使用
- EventBus的使用
- EventBus的使用
- EventBus的简单使用
- OCP 052 -PLSQL
- linux驱动头文件位置的说明
- introduction to gcc
- (iOS App) Bundle 结构 (1)
- mysql中is not null和!=null及null介绍
- EventBus的使用初试
- 跳跃表
- Valgrind 使用简单说明
- 海量数据处理面试题与十个方法大总结分类
- dcxCreator一款优秀的企业级在线DIY平台
- IP XFRM配置:利用linux kernel自带的IPSec实现,手动配置IPSec
- The Linux System Administrator's Guide
- javascript ParseInt函数
- CC1120 433M超远距离无线模块