about how Android detects the type of network, the online code is many, but the useful products are not many, some time ago, because do project need to involve specific network type of check, especially distinguish 2G network is for WAP, or net, type, because WAP type, need to configure proxy Internet, based on this, I'll record the process code I've implemented:
1. Define constants to identify several network types
/**No network*/ Public Static Final intNetworktype_invalid = 0; /**WAP Network*/ Public Static Final intNetworktype_wap = 1; /**2G Network*/ Public Static Final intNETWORKTYPE_2G = 2; /**networks above 3G and 3G, or collectively known as fast networks*/ Public Static Final intNETWORKTYPE_3G = 3; /**WiFi internet*/ Public Static Final intNetworktype_wifi = 4;
2, judge whether it is fastmobilenetwork, will be 3G or more than 3G network called Fast Network
Private Static BooleanIsfastmobilenetwork (Context context) {Telephonymanager Telephonymanager=(Telephonymanager) Context.getsystemservice (Context.telephony_service); Switch(Telephonymanager.getnetworktype ()) { CaseTelephonymanager.network_type_1xrtt:return false;//~ 50-100 kbps CaseTELEPHONYMANAGER.NETWORK_TYPE_CDMA:return false;//~ 14-64 kbps CaseTelephonymanager.network_type_edge:return false;//~ 50-100 kbps Casetelephonymanager.network_type_evdo_0:return true;//~ 400-1000 kbps Casetelephonymanager.network_type_evdo_a:return true;//~ 600-1400 kbps CaseTelephonymanager.network_type_gprs:return false;//~ kbps CaseTELEPHONYMANAGER.NETWORK_TYPE_HSDPA:return true;//~ 2-14 Mbps CaseTelephonymanager.network_type_hspa:return true;//~ 700-1700 kbps CaseTelephonymanager.network_type_hsupa:return true;//~ 1-23 Mbps CaseTelephonymanager.network_type_umts:return true;//~ 400-7000 kbps CaseTELEPHONYMANAGER.NETWORK_TYPE_EHRPD:return true;//~ 1-2 Mbps CaseTelephonymanager.network_type_evdo_b:return true;//~ 5 Mbps CaseTELEPHONYMANAGER.NETWORK_TYPE_HSPAP:return true;//~ 10-20 Mbps CaseTelephonymanager.network_type_iden:return false;//~25 kbps CaseTelephonymanager.network_type_lte:return true;//~ + Mbps CaseTelephonymanager.network_type_unknown:return false; default: return false; } }
3, whether to get the network type is 2G, 3G, WAP, WIFI, etc.
/*** Get network status, wifi,wap,2g,3g. * * @paramContext Contexts *@returnint network Status {@link#NETWORKTYPE_2G},{@link#NETWORKTYPE_3G}, *{@link#NETWORKTYPE_INVALID},{@link#NETWORKTYPE_WAP}* <p>{@link#NETWORKTYPE_WIFI}*/ Public Static intGetnetworktype (Context context) {Connectivitymanager Manager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Manager.getactivenetworkinfo (); if(Networkinfo! =NULL&&networkinfo.isconnected ()) {String type=Networkinfo.gettypename (); if(Type.equalsignorecase ("WIFI") ) {Mnetworktype=Networktype_wifi; } Else if(Type.equalsignorecase ("MOBILE") ) {String proxyhost=Android.net.Proxy.getDefaultHost (); Mnetworktype=textutils.isempty (proxyhost)? (Isfastmobilenetwork (context)?)networktype_3g:networktype_2g): Networktype_wap; } } Else{Mnetworktype=Networktype_invalid; } returnMnetworktype; }
How Android detects the type of network is 3G, 2G, WAP, WiFi