Created
August 21, 2020 15:31
-
-
Save adityabhaskar/da6a66404da09333ba088b70689faaf4 to your computer and use it in GitHub Desktop.
Extension function to check if the device is connected to the internet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Whether the device has an active connection to the internet | |
*/ | |
val Context.isConnected: Boolean | |
get() { | |
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetwork = cm.activeNetwork ?: return false | |
return cm.getNetworkCapabilities(activeNetwork) | |
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment