我有一个场景,我们有一个发射器,它不断发出这样的数据
fun subscribeForEvents(): Flowable<InkChannel> {
return Flowable.create<InkChannel>({
if (inkDevice.availableDeviceServices.contains(DeviceServiceType.EVENT_DEVICE_SERVICE)) {
(inkDevice.getDeviceService(DeviceServiceType.EVENT_DEVICE_SERVICE) as EventDeviceService).subscribe(object : EventCallback {
override fun onUserActionExpected(p0: UserAction?) {
it.onNext(InkChannel.UserActionEvent(p0))
}
override fun onEvent(p0: InkDeviceEvent?, p1: Any?) {
it.onNext(InkChannel.InkEvents<Any>(p0, p1))
}
override fun onUserActionCompleted(p0: UserAction?, p1: Boolean) {
}
}
)
}
}, BackpressureStrategy.BUFFER).share()
}
现在我有一个服务,我在应用程序启动时启动并收听它
inkDeviceBus.subscribeForEvents()
.filter { it -> (it as InkChannel.InkEvents<*>).event == InkDeviceEvent.STATUS_CHANGED }
.map { it -> it as InkChannel.InkEvents<*> }
.map { it -> it.value.toString() }
.filter { value -> value == "CONNECTED" || value == "DISCONNECTED" }
.map { it -> it == "CONNECTED" }
.subscribeBy { b ->
if (b) stopSelf()
}
我有另一个活动 MainActivity 在启动时调用,我观察到相同的事件。现在的问题是只有服务中的侦听器获取事件并且活动没有接收任何事件。
现在,当我从服务中删除侦听器时,活动开始接收事件。我已经使用运营商共享来共享可观察的,但它似乎不起作用