Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created October 28, 2019 13:18
Show Gist options
  • Save cutiko/9379a0cded688ab3961eec381417f3a9 to your computer and use it in GitHub Desktop.
Save cutiko/9379a0cded688ab3961eec381417f3a9 to your computer and use it in GitHub Desktop.
Check if there is internet available
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