Skip to content

Instantly share code, notes, and snippets.

@hissain
Created May 5, 2020 18:20
Show Gist options
  • Select an option

  • Save hissain/7f730a4ffc7cabd3783644da13e912c2 to your computer and use it in GitHub Desktop.

Select an option

Save hissain/7f730a4ffc7cabd3783644da13e912c2 to your computer and use it in GitHub Desktop.
//Constants.REACHABILITY_SERVER = "https://www.google.com"
fun hasInternetConnected(context: Context): Boolean {
if (hasNetworkAvailable(context)) {
try {
val connection = URL(Constants.REACHABILITY_SERVER).openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "ConnectionTest")
connection.setRequestProperty("Connection", "close")
connection.connectTimeout = 1000 // configurable
connection.connect()
Logger.d(classTag, "hasInternetConnected: ${(connection.responseCode == 200)}")
return (connection.responseCode == 200)
} catch (e: IOException) {
Logger.e(classTag, "Error checking internet connection", e)
}
} else {
Log.w(classTag, "No network available!")
}
Log.d(classTag, "hasInternetConnected: false")
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment