Created
May 5, 2020 18:20
-
-
Save hissain/7f730a4ffc7cabd3783644da13e912c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //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