I am writing a program where I need to check three states: 1. If I have no WiFi, 2. if I have WiFi but no internet connection (like if I turn on my router but unplug the Ethernet cable), and 3. if I have WiFi and internet connection. I would then change color of a icon in my app to represent one of these states (red, yellow, or green). Currently condition 2 does not work, anytime I unplug the cable on my router for testing, the icon color changes from green to red.
public static void doPing(Context context) {
String googleUrl = "https://www.google.com";
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);
HttpClient client = new DefaultHttpClient(httpParameters);
if (L) Log.i(TAG, "Calling: " + url );
HttpGet getGoogle = getHttpGet(googleUrl);
HttpResponse responseGoogle = client.execute(getGoogle);
if (responseGoogle != null){
connectionIconView.setIcon(R.drawable.green_wifi);
}
else if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null){
connectionIconView.setIcon(R.drawable.yellow_wifi);
}
else {
connectionIconView.setIcon(R.drawable.red_wifi);
}
} catch(Exception e) {
if (L) Log.e(TAG, "Error during HTTP call");
e.printStackTrace();
}