将广播接收器包装到Flow(协程)中 [英] Wrap Broadcast receiver into Flow (coroutine)
问题描述
我有一个用于接收wifi扫描结果的广播接收器作为数据源,我想以协程方式制作它.我在这里找到了暂停功能的答案: https://stackoverflow.com/a/53520496/5938671
I have a broadcast receiver for wifi scan results as a data source and I'd like to make it in coroutine way. I found an answer for suspend function here: https://stackoverflow.com/a/53520496/5938671
suspend fun getCurrentScanResult(): List<ScanResult> =
suspendCancellableCoroutine { cont ->
//define broadcast reciever
val wifiScanReceiver = object : BroadcastReceiver() {
override fun onReceive(c: Context, intent: Intent) {
if (intent.action?.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) == true) {
context.unregisterReceiver(this)
cont.resume(wifiManager.scanResults)
}
}
}
//setup cancellation action on the continuation
cont.invokeOnCancellation {
context.unregisterReceiver(wifiScanReceiver)
}
//register broadcast reciever
context.registerReceiver(wifiScanReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
//kick off scanning to eventually receive the broadcast
wifiManager.startScan()
}
这对于信号发射是很好的,但是如果我想在扫描进行时获得结果,则会崩溃,因为 cont.resume()
只能被调用一次.然后,我决定尝试 Flow
.这是我的代码:
This is fine for signle emit, but if I want to get results while scanning is going then I'll get crash because cont.resume()
could be called only once. Then I decided to try Flow
. And here is my code:
suspend fun getCurrentScanResult(): Flow<List<ScanResult>> =
flow{
val wifiScanReceiver = object : BroadcastReceiver() {
override fun onReceive(c: Context, intent: Intent) {
if (intent.action?.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) == true) {
//context.unregisterReceiver(this)
emit(wifiManager.scanResults)
}
}
}
//setup cancellation action on the continuation
//register broadcast reciever
context.registerReceiver(wifiScanReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
//kick off scanning to eventually receive the broadcast
wifiManager.startScan()
}
但是现在Android Stuidio表示悬浮函数只能在协程体内调用
是否可以在此处使用Flow?> emit(wifiManager.scanResults)
的函数吗?
But now Android Stuidio says Suspension functions can be called only within coroutine body
for function emit(wifiManager.scanResults)
Is there a way to use Flow here?
推荐答案
Please take a look at the callback flow which is specifically designed for this use case. Something like this will do the job:
callbackFlow {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
sendBlocking(wifiManager.scanResults) // or non-blocking offer()
}
}
}
context.registerReceiver(receiver, intentFilter)
awaitClose {
context.unregisterReceiver(receiver)
}
}
您可能还想与以下人员共享此流程: shareIn 运算符避免为每个流订户注册一个新的接收器.
You also might want to share this flow with e.g. shareIn operator to avoid registering a new receiver for each flow subscriber.
这篇关于将广播接收器包装到Flow(协程)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!