check internet connection doesn't work correctly
this is my code but it doesn't work correctly,it always returns true even
when disable internet connection:
public boolean hasConnection(Context context) {
/**
*context.getsystemservice parameters:
name: The name of the desired service.
Returns: The service or null if the name does not exist.
*/
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork =
cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork =
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
}
return false;
}//end of hasConnection class
No comments:
Post a Comment