Skip to content

Instantly share code, notes, and snippets.

@ishanvohra2
Created April 10, 2024 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishanvohra2/e35303b742d6b568d61e36d69da0588e to your computer and use it in GitHub Desktop.
Save ishanvohra2/e35303b742d6b568d61e36d69da0588e to your computer and use it in GitHub Desktop.
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