此代码总是返回true。 这被当按下登录按钮调用。 我第一次尝试用连接(上仿真器)无线网络连接。 它返回true,然后我断开无线网络连接,然后尝试。 它仍然返回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;
}