I have a specific question about wifi connection in Android.I'm working on a project which is downloading some data from web server and every time before starting the synchronization I'm checking about internet connection like this :
public static boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public static boolean chkNetworkStatus(Context context) {
boolean result = false;
new Thread() {
@Override
public void run() {
for(int i=0;i<3;i++){
HttpGet requestForTest = new HttpGet("http://m.google.com");
try {
new DefaultHttpClient().execute(requestForTest);
responded = true;
} catch (Exception e) {
responded = false;
}
}
}
}.start();
boolean isOnline = isOnline(context);
if(responded && isOnline){
result = true;
} else {
result = false;
}
Log.e("","responded : "+responded);
return result;
}
But in this situation when I'm still connected to wifi and i'm walking (loosing connection) and press sync button it's still returning true because I'm connected, but actually it's not doing anything.
Is there anyway that I can detect this or I should use connectionTimeOut
function in HttpURLConnection
class which I'm using?