Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created May 30, 2020 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/1bed7e360a1ebc9feb6cb80e87c7a013 to your computer and use it in GitHub Desktop.
Save chelseatroy/1bed7e360a1ebc9feb6cb80e87c7a013 to your computer and use it in GitHub Desktop.
Ascertaining Network Connectivity
class LoginActivity : AppCompatActivity() {
...
private fun isNetworkConnected(): Boolean {
val connectivityManager =
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
return networkCapabilities != null &&
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment