java获取网络类型的方法


本文实例讲述了java获取网络类型的方法。分享给大家供大家参考。具体如下:

/**** 
* 获取网络类型 
* 
* @param context 
* @return 
*/ 
public static String getNetType(Context context) { 
try { 
ConnectivityManager connectMgr = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo info = connectMgr.getActiveNetworkInfo(); 
if (info == null) { 
return ""; 
} 
if (info.getType() == ConnectivityManager.TYPE_WIFI) { 
return "WIFI"; 
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { 
if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA) { 
return "CDMA"; 
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) { 
return "EDGE"; 
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0) { 
return "EVDO0"; 
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A) { 
return "EVDOA"; 
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS) { 
return "GPRS"; 
} 
/* 
* else if(info.getSubtype() == 
* TelephonyManager.NETWORK_TYPE_HSDPA){ return "HSDPA"; }else 
* if(info.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPA){ 
* return "HSPA"; }else if(info.getSubtype() == 
* TelephonyManager.NETWORK_TYPE_HSUPA){ return "HSUPA"; } 
*/ 
else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_UMTS) { 
return "UMTS"; 
} else { 
return "3G"; 
} 
} else { 
return ""; 
} 
} catch (Exception e) { 
return ""; 
} 
} 

希望本文所述对大家的java程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3