检查网络的方法二
public static boolean isInternetConnected() {
//拿到connectivityManager
ConnectivityManager connectivityManager = (ConnectivityManager) BXApp.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
//如果connectivityManager为空,返回false
if (null == connectivityManager) return false;
//拿到MOBILE移动网络的networkInfo
NetworkInfo networkInfoMobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (null == networkInfoMobile) return false;
//拿到wifi的networkInfo
NetworkInfo networkInfoWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (null == networkInfoWifi) return false;
//mobile或者wifi有一个连接了就行,否则false
NetworkInfo.State mobile = networkInfoMobile.getState();
NetworkInfo.State wifi = networkInfoWifi.getState();
if (mobile == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}