建了个交流群:416157653,欢迎大家加入讨论
判断手机有无网络的方式:
1. 直接获取手机有没有wifi或数据网络
/*** 有网没网** @param context* @return true: 有网 false:没网*/public static boolean hasNetwork(Context context) { if (isWifiConnected(context)) { return true; } if (isMobileConnected(context)) { return true; } return false;}
/***判断wifi有没有连接*return:true:连上wifi false:未连上wifi*/public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isConnected(); } } return false;}
/***判断数据网络有没有连接*return:true:连接 false:未连接*/public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isConnected(); } } return false;}