-
-
Save ishanvohra2/e35303b742d6b568d61e36d69da0588e to your computer and use it in GitHub Desktop.
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
fun hasNetwork(context: Context): Boolean { | |
val connectivityManager = context | |
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val nw = connectivityManager.activeNetwork ?: return false | |
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false | |
return when { | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true | |
else -> false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment