我有一个用于 wifi 扫描结果的广播接收器作为数据源,我想以协程方式制作它。我在这里找到了暂停功能的答案: 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()
}
Run Code Online (Sandbox Code Playgroud)
这对于信号发射来说很好,但是如果我想在扫描时获得结果,那么我会崩溃,因为cont.resume()只能调用一次。然后我决定尝试一下Flow。这是我的代码:
suspend fun getCurrentScanResult(): Flow<List<ScanResult>> =
flow{
val wifiScanReceiver = …Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-wifi kotlin kotlin-coroutines
我有一个应用程序,我在其中定义了所有必要的详细信息,现在我想将其本地化。我知道我们必须创建值文件夹并添加字符串,其中使用相同的“键”声明其他语言的字符串。
我的问题是假设我的应用程序的标题是“仪表板”,西班牙语是“Tablero de Instrumentos”,知道它包含的字母比英语中的字母多,因此应用程序的对称性会受到干扰。我想要的是根据调用的语言自定义字体大小,这样它就不会影响我的用户界面。