Created
October 28, 2019 13:18
-
-
Save cutiko/9379a0cded688ab3961eec381417f3a9 to your computer and use it in GitHub Desktop.
Check if there is internet available
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 isNoInternet(context: Context): Boolean { | |
val connectionManager = | |
context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager | |
@Suppress("DEPRECATION") | |
return connectionManager?.let { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
it.getNetworkCapabilities(it.activeNetwork)?.run { | |
when { | |
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> false | |
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> false | |
hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> false | |
else -> true | |
} | |
} ?: true | |
} else { | |
it.activeNetworkInfo?.isConnected?.not() ?: true | |
} | |
} ?: return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment