//ConnectivityManager管理网络连接相关的操作
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取代表联网状态的NetWorkInfo对象
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
//activeNetInfo对象不能为空
if(activeNetInfo!=null){
//判断是wifi还是手机网络
if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE){
switch (activeNetInfo.getSubtype()) {
case TelephonyManager.NETWORK_TYPE_GPRS:
//联通2g
break;
case TelephonyManager.NETWORK_TYPE_CDMA:
//电信2g
break;
case TelephonyManager.NETWORK_TYPE_EDGE:
//移动2g
break;
default:
break;
}
}
//判断是否有网络
boolean notewoke = activeNetInfo.isAvailable();
if(notewoke){
Log.e("网络可用", "网络可用");
return true;
}else{
Log.e("网络可用", "网络不bu可用");
return false;
}
}else{
Log.e("网络可用", "网络不bu可用");
return false;
}
}