android my device connected with wifi but how to if wifi is connected but these is no internet connection
following is my code that i trying to check if no internet connection
public static boolean isConnectedWifi(Context context) {
NetworkInfo info=null;
if(context!=null){
info= IsNetConnectionAvailable.getNetworkInfo(context);
}
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
it always return true when no internet access
Best Answer
NetworInfo.isAvailable
andNetworkInfo.isConnected
only indicate whether network connectivity is possible or existed, they can't indicate whether the connected situation has access to the public internet, long story short, they can't tell us the device is online indeed.To check whether a device is online, try the following methods:
First:
Strength: 1. could run on UI thread; 2. fast and accurate.
Weakness: need API >= 23 and compatibility issues.
Second:
Strength: 1. could run on UI thread; 2. you can ping many times and do statistics for min/avg/max delayed time and packet loss rate.
Weakness: compatibility issues.
Third:
Strength: could use on all devices and APIs.
Weakness: time-consuming operation, can't run on UI thread.
Fourth:
Strength: 1. could use on all devices and APIs; 2. relatively fast and accurate.
Weakness: time-consuming operation, can't run on UI thread.