Skip to content

Instantly share code, notes, and snippets.

@Morteza-QN
Created June 13, 2024 17:08
Show Gist options
  • Save Morteza-QN/e952e49da219f1046920f447da90e01f to your computer and use it in GitHub Desktop.
Save Morteza-QN/e952e49da219f1046920f447da90e01f 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