Created
April 7, 2021 13:59
-
-
Save Alqueraf/bddad92b697ea13713764680fa7ab4a6 to your computer and use it in GitHub Desktop.
Android Internet Connectivity
This file contains 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
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