Skip to content

Instantly share code, notes, and snippets.

@lucassales2
Created November 14, 2019 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucassales2/381fcf666a85f9ac4a304a713f3b0a1c to your computer and use it in GitHub Desktop.
Save lucassales2/381fcf666a85f9ac4a304a713f3b0a1c to your computer and use it in GitHub Desktop.
@Suppress("FunctionName")
fun BroadcastReceiverFlow(c: Context, intentFilter: IntentFilter): Flow<Intent> {
return callbackFlow {
val broadcastReceiver = object : android.content.BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
offer(intent)
}
}
c.registerReceiver(broadcastReceiver, intentFilter)
awaitClose { c.unregisterReceiver(broadcastReceiver) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment