Skip to content

Instantly share code, notes, and snippets.

@bapspatil
Last active September 7, 2018 08:06
Show Gist options
  • Save bapspatil/f34e96bcaa697e537e72855e99e151d9 to your computer and use it in GitHub Desktop.
Save bapspatil/f34e96bcaa697e537e72855e99e151d9 to your computer and use it in GitHub Desktop.
A method that checks if the app is connected to the Internet or not.
fun hasNetwork(context: Context): Boolean? {
var isConnected: Boolean? = false // Initial Value
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
if (activeNetwork != null && activeNetwork.isConnected)
isConnected = true
return isConnected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment