-
-
Save verygreenboi/3f5d02106c917f76d13c7b8773f65c5d to your computer and use it in GitHub Desktop.
Check network availability
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
| public static boolean isNetworkAvailable (Context context) { | |
| if (connectedToTheNetwork(context)) { | |
| try { | |
| HttpURLConnection urlc = (HttpURLConnection) | |
| (new URL("http://clients3.google.com/generate_204") | |
| .openConnection()); | |
| urlc.setRequestProperty("User-Agent", "Android"); | |
| urlc.setRequestProperty("Connection", "close"); | |
| urlc.setConnectTimeout(1500); | |
| urlc.connect(); | |
| return (urlc.getResponseCode() == 204 && | |
| urlc.getContentLength() == 0); | |
| } catch (IOException e) { | |
| Log.e(TAG, "Error checking internet connection", e); | |
| } | |
| } else { | |
| Log.d(TAG, "No network available!"); | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment