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 }
注:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。