Skip to content

Instantly share code, notes, and snippets.

@TommyPersson
Last active March 25, 2018 15:15
  • 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/3367fe3caaa455472ed6d630927ed4bd to your computer and use it in GitHub Desktop.
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