判断网络类型是wifi,还是3G,还是2G网络,对不同
的网络进行不同的处理,现将判断方法整理给大家,以供参考
说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上
没有电信的卡,所以没有验证,有电信手机的同事,可以验证一下,验证后将结果
发送给大家。
ConnectivityManager connectMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectMgr.getActiveNetworkInfo();
一、判断网络是否是wifi,在判断之前一定要进行的非空判断,如果没有任何网络
连接info ==null
info.getType() == ConnectivityManager.TYPE_WIFI
二、判断是否是手机网络
info !=null && info.getType() == ConnectivityManager.TYPE_MOBILE
手机网络进行详细区分:
info.getSubtype() 这里使用 getSubtype(),不是 getType(),getType()返回的
是0,或者1,是区分是手机网络还是wifi
info.getSubtype()取值列表如下:
* NETWORK_TYPE_CDMA 网络类型为CDMA
* NETWORK_TYPE_EDGE 网络类型为EDGE
* NETWORK_TYPE_EVDO_0 网络类型为EVDO0
* NETWORK_TYPE_EVDO_A 网络类型为EVDOA
* NETWORK_TYPE_GPRS 网络类型为GPRS
* NETWORK_TYPE_HSDPA 网络类型为HSDPA
* NETWORK_TYPE_HSPA 网络类型为HSPA
* NETWORK_TYPE_HSUPA 网络类型为HSUPA
* NETWORK_TYPE_UMTS 网络类型为UMTS
联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EDGE,电信的2G为CDMA,电信
的3G为EVDO
android获取手机的ip地址
private String getPhoneIp() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { } return ""; }
/** * 判断是否是2G网络 * * @param context * @return */ @SuppressLint("NewApi") public boolean getNetWorkType() { boolean is2G = false; NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { Logger.d("NetworkStatus", "Network Type:" + " WIFI"); is2G = false; } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { String subTypeName = networkInfo.getSubtypeName(); int networkType = networkInfo.getSubtype(); Logger.d("NetworkStatus", "Network SubtypeName: " + subTypeName + " Subtype: " + networkType); switch (networkType) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: // api<8 : replace by // 11 Logger.d("NetworkStatus", "Network Type:" + " 2G"); is2G = true; break; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: // api<9 : replace by // 14 case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11 : replace by // 12 case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13 : replace by // 15 // 3G Logger.d("NetworkStatus", "Network Type:" + " 3G"); is2G = false; break; case TelephonyManager.NETWORK_TYPE_LTE: // api<11 : replace by // 13 // 4G Logger.d("NetworkStatus", "Network Type:" + "4G"); is2G = false; break; default: // 中国移动 联通 电信 三种3G制式 if (subTypeName.equalsIgnoreCase("TD-SCDMA") || subTypeName.equalsIgnoreCase("WCDMA") || subTypeName.equalsIgnoreCase("CDMA2000")) { // 3G Logger.d("NetworkStatus", "Network Type:" + " 3G"); is2G = false; } else { Logger.d("NetworkStatus", "Network Type:" + " 2G"); is2G = true; } break; } } } else { Logger.d("NetworkStatus", "Network Type:" + " 2G"); } return is2G; }