记录一个获取手机当前联网状态的方法,可以当作工具直接使用,比较方便。
private String getNetType(Context context) { String type = ""; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info == null) { type = "null"; } else if (info.getType() == ConnectivityManager.TYPE_WIFI) { type = "wifi"; } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { int subType = info.getSubtype(); if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS || subType == TelephonyManager.NETWORK_TYPE_EDGE) { type = "2g"; } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA || subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0 || subType == TelephonyManager.NETWORK_TYPE_EVDO_B) { type = "3g"; } else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡,是3.9G的全球标准 type = "4g"; } } return type;}
参考:http://blog.sina.com.cn/s/blog_5da93c8f0102vg21.html