Created
February 1, 2020 07:37
-
-
Save ronnieotieno/f4f6881b5aa40d6c061ce3ead305531d 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
private fun isInternetAvailable(context: Context): Boolean { | |
var result = false | |
val connectivityManager = | |
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
val networkCapabilities = connectivityManager.activeNetwork ?: return false | |
val actNw = | |
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false | |
result = when { | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | |
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | |
else -> false | |
} | |
} else { | |
connectivityManager.run { | |
connectivityManager.activeNetworkInfo?.run { | |
result = when (type) { | |
ConnectivityManager.TYPE_WIFI -> true | |
ConnectivityManager.TYPE_MOBILE -> true | |
ConnectivityManager.TYPE_ETHERNET -> true | |
else -> false | |
} | |
} | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment