这段代码总是返回true。当用户按下登录按钮时,它会被调用。我最初在模拟器上尝试连接WiFi(wifi connected),它返回true。然后我断开了WiFi并再次尝试,它仍然返回true。
public static boolean isNetworkAvailable( Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
State network = networkInfo.getState();
Log.d("here", "true");
return (network == NetworkInfo.State.CONNECTED || network == NetworkInfo.State.CONNECTING);
}
Log.d("here1", "false");
return false;
}