首先工程Manifest文件要引用:android
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 }
//網絡型號ide
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 }
調用:spa
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 }
注:code
能夠參考資料:blog
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 ip