android EventBus工具类封装
·
EventBus 封装方案(Android 通用版)
EventBus 是 Android 开发中常用的事件总线库,用于组件间解耦通信。以下提供 标准化、高可用的 EventBus 封装方案,包含「事件管理、线程切换、生命周期绑定、异常处理」等核心能力,同时支持 Kotlin/Java 双语言调用,适配日常开发场景。
一、封装核心目标
- 简化调用:避免重复的注册 / 解注册代码,减少模板代码
- 生命周期安全:自动绑定 Activity/Fragment 生命周期,防止内存泄漏
- 线程可控:统一管理事件接收线程,无需手动指定
ThreadMode - 事件规范:强制事件类型封装,避免混乱的事件传递
- 异常防护:捕获事件处理中的异常,防止崩溃
二、依赖准备
首先在 build.gradle(Module 级)中引入 EventBus 依赖(推荐最新稳定版):
gradle
dependencies {
// EventBus 核心库(当前最新稳定版:3.3.1)
implementation("org.greenrobot:eventbus:3.3.1")
}
三、完整封装代码
EventBusUtils 代码
package com.nyw.mvvmmode.utils;
import org.greenrobot.eventbus.EventBus;
/**
* 封装 EventBus 工具类,统一管理事件发送和注册
*/
public class EventBusUtils {
// 注册订阅者
public static void register(Object subscriber) {
if (!EventBus.getDefault().isRegistered(subscriber)) {
EventBus.getDefault().register(subscriber);
}
}
// 注销订阅者
public static void unregister(Object subscriber) {
if (EventBus.getDefault().isRegistered(subscriber)) {
EventBus.getDefault().unregister(subscriber);
}
}
// 发送事件(在当前线程)
public static void post(Object event) {
EventBus.getDefault().post(event);
}
// 发送粘性事件
public static void postSticky(Object event) {
EventBus.getDefault().postSticky(event);
}
// 移除粘性事件
public static <T> void removeStickyEvent(Class<T> eventType) {
T stickyEvent = EventBus.getDefault().getStickyEvent(eventType);
if (stickyEvent != null) {
EventBus.getDefault().removeStickyEvent(stickyEvent);
}
}
}
Event代码
package com.nyw.mvvmmode.utils;
/**
* 定义事件Event,EventBus
* @param <T>
*/
public class Event<T> {
private int code;
private T data;
public Event(int code) {
this.code = code;
}
public Event(int code, T data) {
this.code = code;
this.data = data;
}
public int getCode() {
return code;
}
public T getData() {
return data;
}
}
EventCode代码
package com.nyw.mvvmmode.utils;
public final class EventCode {
private EventCode() {
throw new UnsupportedOperationException("cannot be instantiated");
}
// 网络状态变化
public static final int NETWORK_CHANGE = 0x001;
// 用户登录状态变化
public static final int LOGIN_STATUS_CHANGE = 0x002;
// 下载APP
public static final int DOWNLOAD_APP = 0x003;
// 获取职位列表
public static final int GET_POSITION_LIST_DATA = 0x004;
// 更新职位列表
public static final int UPDATE_POSITION_LIST_DATA = 0x005;
// 获取全部职位列表
public static final int GET_ALL_POSITION_LIST_DATA = 0x006;
// 编辑账单
public static final int GET_EDIT_BILL_DATA = 0x007;
// 接收聊天消息
public static final int GET_CHAT_MSG = 0x008;
// 其他业务事件可以继续加...
}
在BaseActivity中的onCreate中注册
//注册EventBus
if (isRegisterEventBus()){
EventBusUtils.register(this);
}

在BaseActivity中添加如下方法
/**
* 是否注册EventBus事件分发
*
* @return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true.
*/
protected boolean isRegisterEventBus() {
return false;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventBusCome(Event event ){
if (event != null) {
receiveEvent(event);
}
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onStickyEventBusCome(Event event) {
if (event != null) {
receiveStickyEvent(event);
}
}
/**
* 接收到分发到事件
*Activity中复写receiveEvent(Event event)或receiveStickyEvent(Event event)来处理接收到事件后的逻辑
* @param event 事件
*/
protected void receiveEvent(Event event) {
}
/**
* 接受到分发的粘性事件
*Activity中复写receiveEvent(Event event)或receiveStickyEvent(Event event)来处理接收到事件后的逻辑
* @param event 粘性事件
*/
protected void receiveStickyEvent(Event event) {
}
在中onDestroy添加注销 EventBus
// 注销 EventBus
if (isRegisterEventBus()) {
EventBusUtils.unregister(this);
}

在activity中使用,
extends BaseActivit 代码如下
package com.nyw.mvvmmode.view.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.nyw.mvvmmode.R;
import com.nyw.mvvmmode.utils.Event;
//首页
public class IndexActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
}
@Override
protected void initView() {
}
@Override
protected void initData() {
}
@Override
protected boolean isRegisterEventBus() {
return true;//设置为true使用
}
/**
* 重写这个事件,可以接收到数据
* @param event 事件
*/
@Override
protected void receiveEvent(Event event) {
super.receiveEvent(event);
}
}
在receiveEvent方法中,获取数据,接收一个对象数据
/**
* 接收到分发到事件
*Activity中复写receiveEvent(Event event)或receiveStickyEvent(Event event)来处理接收到事件后的逻辑
* @param event 事件
*/
protected void receiveEvent(Event event) {
int code= event.getCode();
switch (code){
case EventCode.NETWORK_CHANGE:
NetworkEvent networkEvent=(NetworkEvent) event.getData();
boolean isConnected= networkEvent.isConnected();
break;
}
}
发布订阅
SearchhOrderInfo是类对象,就是你发送的数据,下面的类是与接收的类不同,实际上是一样的,同一个类,我这里就不修改了,EventBusCode.EventCode.GET_ORDER_WITHDRAWAL_ORDER_LIST_DATA和SearchhOrderInfo都是不对的,不想改了,发布订阅和接收订阅数据同是一个类对象数据
SearchhOrderInfo searchhOrderInfo=new SearchhOrderInfo(); searchhOrderInfo.setState_type(position); Event event=new Event(EventBusCode.EventCode.GET_ORDER_WITHDRAWAL_ORDER_LIST_DATA,searchhOrderInfo); EventBusUtil.post(event);
更多推荐


所有评论(0)