Android 如何使用绿色机器人&x27;s EventBus将事件从服务广播到活动?
最近我意识到了EventBus库。基本上,我的用例围绕服务和活动展开 服务用于跟踪BLE连接中的更改 活动用于向UI报告该连接状态Android 如何使用绿色机器人&x27;s EventBus将事件从服务广播到活动?,android,bluetooth-lowenergy,greenrobot-eventbus,Android,Bluetooth Lowenergy,Greenrobot Eventbus,最近我意识到了EventBus库。基本上,我的用例围绕服务和活动展开 服务用于跟踪BLE连接中的更改 活动用于向UI报告该连接状态 如何使用库实现相同的功能。在活动的onResume方法中,注册事件: EventBus.getDefault().register(this); 并在暂停时取消注册 EventBus.getDefault().unregister(this); 当服务运行并获取有关BLE的信息时,通过EventBus发送此信息: BLEInfo bleInfo = new BL
如何使用库实现相同的功能。在活动的
onResume
方法中,注册事件:
EventBus.getDefault().register(this);
并在暂停时取消注册
EventBus.getDefault().unregister(this);
当服务运行并获取有关BLE的信息时,通过EventBus发送此信息:
BLEInfo bleInfo = new BLEInfo(... // create some kind of object to aggregate the info about ble connection
EventBus.getDefault().post(bleInfo);
最后,实现活动获取信息的行为:
public void onEvent(BLEInfo bleInfo) {
// update your UI based on bleInfo
}