此代码始终返回 true。当按下登录按钮时调用它。我首先尝试连接wifi(在模拟器上)。它返回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;
}