Skip to content

Instantly share code, notes, and snippets.

@alistairsykes
Last active October 1, 2021 18:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alistairsykes/5034fa7133e246df04b78bedd17d3a50 to your computer and use it in GitHub Desktop.
Save alistairsykes/5034fa7133e246df04b78bedd17d3a50 to your computer and use it in GitHub Desktop.
import android.app.Application
import android.arch.lifecycle.LiveData
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkInfo
import android.net.NetworkRequest
import android.os.Build
import android.support.annotation.RequiresPermission
import android.support.annotation.VisibleForTesting
/**
* A LiveData class which wraps the network connection status
* Requires Permission: ACCESS_NETWORK_STATE
*
* See https://developer.android.com/training/monitoring-device-state/connectivity-monitoring
* See https://developer.android.com/reference/android/net/ConnectivityManager
* See https://developer.android.com/reference/android/net/ConnectivityManager#CONNECTIVITY_ACTION
*/
class ConnectivityLiveData @VisibleForTesting internal constructor(private val connectivityManager: ConnectivityManager)
: LiveData<Boolean>() {
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
constructor(application: Application) : this(application.getSystemService(Context.CONNECTIVITY_SERVICE)
as ConnectivityManager)
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network?) {
postValue(true)
}
override fun onLost(network: Network?) {
postValue(false)
}
}
override fun onActive() {
super.onActive()
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
postValue(activeNetwork?.isConnectedOrConnecting == true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(networkCallback)
} else {
val builder = NetworkRequest.Builder()
connectivityManager.registerNetworkCallback(builder.build(), networkCallback)
}
}
override fun onInactive() {
super.onInactive()
connectivityManager.unregisterNetworkCallback(networkCallback)
}
}
@webserveis
Copy link

webserveis commented Aug 23, 2019

Yes, at the moment what is found works in all versions of Android, the alternative is to use .isRecheable but it does not offer the same reliability
I see in. https://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address

@alistairsykes
Copy link
Author

Very interesting. Thank you for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment