BroadcastReceiverObservable

简介:Turn a BroadcastReceiver into an Observable
更多:作者   提 Bug   
标签:

Turn a BroadcastReceiver into an RxJava Observable

Build Status

Usage

To start listening for connectivity change events:

val text = findViewById<TextView>(R.id.text)

val context = applicationContext
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)

BroadcastReceiverObservable.create(context, filter)
        .startWith(Intent())
        .map {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val activeNetwork = cm.activeNetworkInfo
            activeNetwork != null && activeNetwork.isConnected
        }
        .distinctUntilChanged()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { connected ->
            text.text = if (connected) "connected" else "disconnected"
        }

It is recommended that you use some method to cancel the subscription when the activity or fragment is destroyed so that the BroadcastReceiver gets unregistered. RxLifecycle is a great tool for this.

Acknowledgements

BroadcastReceiverObservable was inspired by rxnetwork-android and modified a bit to make it work with RxJava 2.

License

Copyright 2017 Commit 451

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
随手点击下面广告,支持我维护本站吧
随手点击下面广告支持我免费维护吧
Android 开发经验分享
随手点击下面广告支持本站吧
 
Android 开发经验分享