Skip to content

Instantly share code, notes, and snippets.

@adityabhaskar
Created August 21, 2020 15:31
Show Gist options
  • Save adityabhaskar/da6a66404da09333ba088b70689faaf4 to your computer and use it in GitHub Desktop.
Save adityabhaskar/da6a66404da09333ba088b70689faaf4 to your computer and use it in GitHub Desktop.
Extension function to check if the device is connected to the internet
/**
* 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