Skip to content

Instantly share code, notes, and snippets.

@TommyPersson
Last active March 25, 2018 15:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TommyPersson/60a51ef8dfa6e4360196c42120d03d34 to your computer and use it in GitHub Desktop.
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