Mel Mel - 5 months ago 49
Android Question

How to detect when WIFI is connected to internet?

I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.

My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.

The code

protected boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {

return true;

else {
return false;


} //end checkInterneConnection method

Thanks for your time.


Answer Source

You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download