Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private var connectivitySubscription: Disposable? = null
override fun onStart() {
super.onStart()
connectivitySubscription = observeBroadcasts(ConnectivityManager.CONNECTIVITY_ACTION)
.subscribe(this::onConnectivityChange)
}
override fun onStop() {
super.onStop()
connectivitySubscription?.dispose()
}
private fun onConnectivityChange(intent: Intent) {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val isConnected = connectivityManager.activeNetworkInfo?.isConnected ?: false
Log.i("MainActivity", "isConnected = $isConnected")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment