android 获取网络类型名称2G 3G 4G wifi
首先工程Manifest文件要引用:
1 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 2 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//获取网络服务
1 public static String getNetworkTypeName(Context context) { 2 if (context != null) { 3 ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 4 if (connectMgr != null) { 5 NetworkInfo info = connectMgr.getActiveNetworkInfo(); 6 if (info != null) { 7 switch (info.getType()) { 8 case ConnectivityManager.TYPE_WIFI: 9 return "WIFI"; 10 case ConnectivityManager.TYPE_MOBILE: 11 return getNetworkTypeName(info.getSubtype()); 12 } 13 } 14 } 15 } 16 return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN); 17 }
//网络型号
1 public static String getNetworkTypeName(int type) { 2 switch (type) { 3 case TelephonyManager.NETWORK_TYPE_GPRS: 4 return "GPRS"; 5 case TelephonyManager.NETWORK_TYPE_EDGE: 6 return "EDGE"; 7 case TelephonyManager.NETWORK_TYPE_UMTS: 8 return "UMTS"; 9 case TelephonyManager.NETWORK_TYPE_HSDPA: 10 return "HSDPA"; 11 case TelephonyManager.NETWORK_TYPE_HSUPA: 12 return "HSUPA"; 13 case TelephonyManager.NETWORK_TYPE_HSPA: 14 return "HSPA"; 15 case TelephonyManager.NETWORK_TYPE_CDMA: 16 return "CDMA"; 17 case TelephonyManager.NETWORK_TYPE_EVDO_0: 18 return "CDMA - EvDo rev. 0"; 19 case TelephonyManager.NETWORK_TYPE_EVDO_A: 20 return "CDMA - EvDo rev. A"; 21 case TelephonyManager.NETWORK_TYPE_EVDO_B: 22 return "CDMA - EvDo rev. B"; 23 case TelephonyManager.NETWORK_TYPE_1xRTT: 24 return "CDMA - 1xRTT"; 25 case TelephonyManager.NETWORK_TYPE_LTE: 26 return "LTE"; 27 case TelephonyManager.NETWORK_TYPE_EHRPD: 28 return "CDMA - eHRPD"; 29 case TelephonyManager.NETWORK_TYPE_IDEN: 30 return "iDEN"; 31 case TelephonyManager.NETWORK_TYPE_HSPAP: 32 return "HSPA+"; 33 default: 34 return "UNKNOWN"; 35 } 36 }
调用:
1 @Override 2 public void onClick(View view) { 3 // TODO Auto-generated method stub 4 switch (view.getId()) { 5 case R.id.btn_main: 6 String type =getNetworkTypeName(context); 7 if (type!=null){ 8 if ( type.equals("GPRS") || type.equals("EGDE") ) 9 type ="2G"; 10 else if (type.equals("CDMA")) 11 type ="电信 2G"; 12 else if (type.equals("EVDO")) 13 type ="电信 3G"; 14 else if (type.equals("LTE")) 15 type ="电信 4G"; 16 else if (type.equals("HSDPA") ||type.equals("UMTS") ) 17 type ="3G"; 18 19 } 20 btn_main.setText(type); 21 break; 22 }
注:
可以参考资料:
GPRS 2G(2.5) General Packet Radia Service 114kbps
EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
UMTS 3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
CDMA 2G 电信 Code Division Multiple Access 码分多址
EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
EVDO_A 3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
HSDPA 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
HSUPA 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access
IDEN 2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
EHRPD 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
HSPAP 3G HSPAP 比 HSDPA