Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun Context.observeBroadcasts(action: String): Observable<Intent> {
return observeBroadcasts(IntentFilter(action))
}
fun Context.observeBroadcasts(intentFilter: IntentFilter): Observable<Intent> {
val observable = Observable.create<Intent> { observer ->
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
observer.onNext(intent)
}
}
observer.setDisposable(Disposables.fromRunnable {
unregisterReceiver(receiver)
})
registerReceiver(receiver, intentFilter)
}
return observable
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.