Created
August 1, 2018 12:23
-
-
Save rokon12/f27db533028c459fbca1118d2db0fecc to your computer and use it in GitHub Desktop.
How to check if internet is available from internet application
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 isOnline(Context context) { | |
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (connectivityManager != null) { | |
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | |
return activeNetworkInfo != null && activeNetworkInfo.isConnected() && isInternetAvailable(); | |
} | |
return false; | |
} | |
public static boolean isInternetAvailable() { | |
return isHostAvailable("bkash.com") | |
|| isHostAvailable("google.com") | |
|| isHostAvailable("amazon.com") | |
|| isHostAvailable("facebook.com") | |
|| isHostAvailable("apple.com"); | |
} | |
private static boolean isHostAvailable(String hostName) { | |
try (Socket socket = new Socket()) { | |
int port = 80; | |
InetSocketAddress socketAddress = new InetSocketAddress(hostName, port); | |
socket.connect(socketAddress, 3000); | |
return true; | |
} catch (IOException unknownHost) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment