A valid network connection does not necessarily mean that you can access the Internet, and the OP asks for ways to check the availability of an Internet connection . In some Wi-Fi networks, it may be necessary for users to open a browser and go through some kind of authorization before allowing access to the Internet, so in this case it is not enough to check the actual WiFi connection.
A naive approach to checking raw communication without creating a GET / POST connection:
private boolean isConnected() { try { InetAddress.getByName("google.com"); } catch (UnknownHostException e) { return false; } return true; }
warning1 : After some testing, I must warn that the code above DOES NOT WORK, even if it appears at the beginning a couple of times.
The code tries to resolve a host name that is not already in the DNS cache, so after a successful cache search, the code will continue to return true, even if the host is unavailable, since the whole modern OS performs DNS caching.
BUT ... On Android, the InetAddress class has an additional method that is not found in the standard JavaSE - InetAddress.isReachable (int timeout) , which according to javadocs:
"Trying to achieve this InetAddress. First, this method tries to use ICMP (ICMP ECHO REQUEST), returning to the TCP connection on port 7 (Echo) of the remote host."
So a possible fix for the above code would be
InetAddress.getByName("google.com").isReachable(5000);
If ICMP packets are blocked, the only reliable way to check availability is to create a real connection to your host, either through URLConnection , HttpClient , Socket, or anything in between.