Skip to content

Instantly share code, notes, and snippets.

@Alqueraf
Created April 7, 2021 13:59
Show Gist options
  • Save Alqueraf/bddad92b697ea13713764680fa7ab4a6 to your computer and use it in GitHub Desktop.
Save Alqueraf/bddad92b697ea13713764680fa7ab4a6 to your computer and use it in GitHub Desktop.
Android Internet Connectivity
private fun hasNetwork(context: Context): Boolean {
var result = false
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
cm?.run {
cm.getNetworkCapabilities(cm.activeNetwork)?.run {
result = when {
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment