Android学习系列(43)--使用事件总线框架EventBus和Otto
事件总线框架
针对事件提供统一订阅,发布以达到组件间通信的解决方案。
原理
观察者模式。
EventBus和Otto
先看EventBus的官方定义:
Android optimized event bus that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
再看Otto官方定义:
Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.
总之,简化android应用内组件通信。
对比BroadcastReceiver
在工作上,我在两个场景下分别使用过Otto和EventBus,一个是下载管理器通知各个相关的Activity当前的进度,一个是设置应用壁纸。
单从使用上看,EventBus > Otto > BroadcastReceiver(当然BroadcastReceiver作为系统内置组件,有一些前两者没有的功能).
EventBus最简洁,Otto最符合Guava EventBus的设计思路, BroadcastReceiver最难使用。
我个人的第一选择是EventBus。
实例:“设置壁纸”
两大的框架的基本使用都非常简单:
EventBus的基本使用官方参考:https://github.com/greenrobot/EventBus
Otto的基本使用官方参考:http://square.github.io/otto/
EventBus实现篇
EventBus规定onEvent方法固定作为订阅者接受事件的方法,应该是参考了“约定优于配置”思想。
- 定义EventModel,作为组件间通信传递数据的载体
public class WallpaperEvent {
private Drawable wallpaper;
public WallpaperEvent(Drawable wallpaper) {
this.wallpaper = wallpaper;
}
public Drawable getWallpaper() {
return wallpaper;
}
public void setWallpaper(Drawable wallpaper) {
this.wallpaper = wallpaper;
}
}
- 定义订阅者,最重要的是onEvent方法
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
initWallpaper();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void onEvent(WallpaperEvent wallpaperEvent) {
// AppConfig.sWallpaperDrawable as a global static var
AppConfig.sWallpaperDrawable = wallpaperEvent.getWallpaper();
initWallpaper();
}
private void initWallpaper() {
// support custom setting the wallpaper
// 根据AppConfig.sWallpaperDrawable,默认值等设置当前Activity的背景壁纸
// ...
}
}
- 通过post()方法在任何地方发布消息(壁纸,准确的说是WallpaperEvent)给所有的BaseActivity子类,举个例子:
private void downloadWallpapper(String src) {
ImageLoader.getInstance().loadImage(src, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
BitmapDrawable wallpaper = new BitmapDrawable(loadedImage);
// presist the image url for cache
saveWallpaper(imageUri);
// notify all base activity to update wallpaper
EventBus.getDefault().post(new WallpaperEvent(wallpaper));
Toast.makeText(WallpapeEventBusrActivity.this,
R.string.download_wallpaper_success,
Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
Toast.makeText(WallpaperActivity.this,
R.string.download_wallpaper_fail,
Toast.LENGTH_SHORT).show();
}
});
}
重点就是这句:
// 在任何地方调用下面的方法,即可动态全局实现壁纸设置功能
EventBus.getDefault().post(new WallpaperEvent(wallpaper));
Otto实现篇
这里要注意几点点:
(1)Otto使用注解定义订阅/发布者的角色,@Subscribe为订阅者,@Produce为发布者,方法名称就可以自定义了。
(2)Otto为了性能,代码意图清晰,@Subscribe,@Produce方法必须定义在直接的作用类上,而不能定义在基类而被继承。
(3)和EventBus不同的是,发布者也需要register和unregister,而EventBus的发布者是不需要的。
- 定义EventModel,作为组件间通信传递数据的载体
public class WallpaperEvent {
private Drawable wallpaper;
public WallpaperEvent(Drawable wallpaper) {
this.wallpaper = wallpaper;
}
public Drawable getWallpaper() {
return wallpaper;
}
public void setWallpaper(Drawable wallpaper) {
this.wallpaper = wallpaper;
}
}
- 避免浪费,相对于EventBus.getDefault(), Otto需要自己实现单例。
public class AppConfig {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
}
- 定义订阅者,在接受事件的方法加上修饰符@Subscribe
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppConfig.getBusInstance().register(this);
initWallpaper();
}
@Override
protected void onDestroy() {
super.onDestroy();
AppConfig.getBusInstance().unregister(this);
}
public void onOttoEvent(WallpaperEvent wallpaperEvent) {
AppConfig.sWallpaperDrawable = wallpaperEvent.getWallpaper();
initWallpaper();
}
private void initWallpaper() {
// support custom setting the wallpaper
// 根据AppConfig.sWallpaperDrawable,默认值等设置当前Activity的背景壁纸
// ...
}
}
- 定义发布者,通过post()方法在任何地方发布消息了
public class WallpaperActivity extends BaseActivity {
private Drawable wallpaperDrawable;
//这里同时也要更新自己壁纸,所以显示定义@Subscribe的方法
@Subscribe
public void onWallpaperUpdate(WallpaperEvent wallpaperEvent) {
super.onWallpaperUpdate(wallpaperEvent);
}
@Produce
public WallpaperEvent publishWallPaper() {
return new WallpaperEvent(wallpaperDrawable);
}
private void downloadWallpapper(String src) {
//...
//通知所有@Subscribe匹配WallpaperEvent参数的方法执行
AppConfig.getBusInstance().post(publishWallPaper());
//...
}
}
小结
- 使用设计模式的思想解决问题,这才是设计模式的真正价值。
- 这两个android事件总线框架提供了一种更灵活更强大而又更加完美解耦的解决方案,在很多场合,从开发效率,执行性能和设计思路上都要优于BroadcastReceiver,值得学习使用。
分类:
Android学习系列
标签:
Android
, Android学习系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构