How Android detects network types of 3G, 2G, wap, wifi

Keywords: network Android Mobile

There are many online codes about how Android detects the type of network, but there are few useful products. Some time ago, because the project needs to involve specific network type checks, especially to distinguish the type of 2G network for WAP or net. Because of the type of wap, we need to configure proxy to access the Internet. Based on this, I record the process code I implemented myself:

1. Define constants to identify several network types

 

  1. /** No network*/  
  2. public static final int NETWORKTYPE_INVALID = 0;  
  3. /** wap Network*/  
  4. public static final int NETWORKTYPE_WAP = 1;  
  5. /** 2G Network*/  
  6. public static final int NETWORKTYPE_2G = 2;  
  1. /** 3G Networks over 3G, or collectively referred to as fast networks*/  
  2. public static final int NETWORKTYPE_3G = 3;  
  1. /** wifi Network*/  
  2. public static final int NETWORKTYPE_WIFI = 4;  


2. Judging whether it is FastMobile NetWork, which calls a network with 3G or more as a fast network?

  1. private static boolean isFastMobileNetwork(Context context) {  
  2. elephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
  3. switch (telephonyManager.getNetworkType()) {  
  4.        case TelephonyManager.NETWORK_TYPE_1xRTT:  
  5.            return false// ~ 50-100 kbps  
  6.        case TelephonyManager.NETWORK_TYPE_CDMA:  
  7.            return false// ~ 14-64 kbps  
  8.        case TelephonyManager.NETWORK_TYPE_EDGE:  
  9.            return false// ~ 50-100 kbps  
  10.        case TelephonyManager.NETWORK_TYPE_EVDO_0:  
  11.            return true// ~ 400-1000 kbps  
  12.        case TelephonyManager.NETWORK_TYPE_EVDO_A:  
  13.            return true// ~ 600-1400 kbps  
  14.        case TelephonyManager.NETWORK_TYPE_GPRS:  
  15.            return false// ~ 100 kbps  
  16.        case TelephonyManager.NETWORK_TYPE_HSDPA:  
  17.            return true// ~ 2-14 Mbps  
  18.        case TelephonyManager.NETWORK_TYPE_HSPA:  
  19.            return true// ~ 700-1700 kbps  
  20.        case TelephonyManager.NETWORK_TYPE_HSUPA:  
  21.            return true// ~ 1-23 Mbps  
  22.        case TelephonyManager.NETWORK_TYPE_UMTS:  
  23.            return true// ~ 400-7000 kbps  
  24.        case TelephonyManager.NETWORK_TYPE_EHRPD:  
  25.            return true// ~ 1-2 Mbps  
  26.        case TelephonyManager.NETWORK_TYPE_EVDO_B:  
  27.            return true// ~ 5 Mbps  
  28.        case TelephonyManager.NETWORK_TYPE_HSPAP:  
  29.            return true// ~ 10-20 Mbps  
  30.        case TelephonyManager.NETWORK_TYPE_IDEN:  
  31.            return false// ~25 kbps  
  32.        case TelephonyManager.NETWORK_TYPE_LTE:  
  33.            return true// ~ 10+ Mbps  
  34.        case TelephonyManager.NETWORK_TYPE_UNKNOWN:  
  35.            return false;  
  36.        default:  
  37.            return false;  
  38.     }  
  39. }  


3. Is the access network type 2G, 3G, wap, wifi, etc?

  1. /** 
  2.      * Get the network status, wifi,wap,2g,3g. 
  3.      * 
  4.      * @param context context 
  5.      * @return int Network status {@link#NETWORKTYPE_2G},{@link#NETWORKTYPE_3G}, * {@link#NETWORKTYPE_INVALID},{@link#NETWORKTYPE_WAP}* <p> {@link#NETWORKTYPE_WIFI} 
  6.      */  
  7.   
  8.     public static int getNetWorkType(Context context) {  
  9.   
  10.         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  11.         NetworkInfo networkInfo = manager.getActiveNetworkInfo();  
  12.   
  13.         if (networkInfo != null && networkInfo.isConnected()) {  
  14.             String type = networkInfo.getTypeName();  
  15.   
  16.             if (type.equalsIgnoreCase("WIFI")) {  
  17.                 mNetWorkType = NETWORKTYPE_WIFI;  
  18.             } else if (type.equalsIgnoreCase("MOBILE")) {  
  19.                 String proxyHost = android.net.Proxy.getDefaultHost();  
  20.   
  21.                 mNetWorkType = TextUtils.isEmpty(proxyHost)  
  22.                         ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G)  
  23.                         : NETWORKTYPE_WAP;  
  24.             }  
  25.         } else {  
  26.             mNetWorkType = NETWORKTYPE_INVALID;  
  27.         }  
  28.   
  29.         return mNetWorkType;  
  30.     }   

Posted by JAY6390 on Tue, 05 Feb 2019 20:30:17 -0800