Created
August 13, 2021 09:42
-
-
Save davidelp68/0603a5e038f6f211d7e3dd8f25a063a9 to your computer and use it in GitHub Desktop.
Android Studio - WebView
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 isConnected(context: Context): Boolean | |
{ | |
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
////per versioni Android uguali o superiori ad Android M (Android 6.0 - API 23) | |
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 | |
//per altri dispositivi che sono in grado di connettersi con Ethernet | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | |
//per controllare la connessione Internet tramite Bluetooth | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true | |
else -> false | |
} | |
} | |
else //per versioni Android inferiori ad Android M (Android 6.0 - API 23) | |
{ | |
return connectivityManager.activeNetworkInfo?.isConnected ?: false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment