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
- /** No network*/
- public static final int NETWORKTYPE_INVALID = 0;
- /** wap Network*/
- public static final int NETWORKTYPE_WAP = 1;
- /** 2G Network*/
- public static final int NETWORKTYPE_2G = 2;
- /** 3G Networks over 3G, or collectively referred to as fast networks*/
- public static final int NETWORKTYPE_3G = 3;
- /** wifi Network*/
- 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?
- private static boolean isFastMobileNetwork(Context context) {
- elephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
- switch (telephonyManager.getNetworkType()) {
- case TelephonyManager.NETWORK_TYPE_1xRTT:
- return false; // ~ 50-100 kbps
- case TelephonyManager.NETWORK_TYPE_CDMA:
- return false; // ~ 14-64 kbps
- case TelephonyManager.NETWORK_TYPE_EDGE:
- return false; // ~ 50-100 kbps
- case TelephonyManager.NETWORK_TYPE_EVDO_0:
- return true; // ~ 400-1000 kbps
- case TelephonyManager.NETWORK_TYPE_EVDO_A:
- return true; // ~ 600-1400 kbps
- case TelephonyManager.NETWORK_TYPE_GPRS:
- return false; // ~ 100 kbps
- case TelephonyManager.NETWORK_TYPE_HSDPA:
- return true; // ~ 2-14 Mbps
- case TelephonyManager.NETWORK_TYPE_HSPA:
- return true; // ~ 700-1700 kbps
- case TelephonyManager.NETWORK_TYPE_HSUPA:
- return true; // ~ 1-23 Mbps
- case TelephonyManager.NETWORK_TYPE_UMTS:
- return true; // ~ 400-7000 kbps
- case TelephonyManager.NETWORK_TYPE_EHRPD:
- return true; // ~ 1-2 Mbps
- case TelephonyManager.NETWORK_TYPE_EVDO_B:
- return true; // ~ 5 Mbps
- case TelephonyManager.NETWORK_TYPE_HSPAP:
- return true; // ~ 10-20 Mbps
- case TelephonyManager.NETWORK_TYPE_IDEN:
- return false; // ~25 kbps
- case TelephonyManager.NETWORK_TYPE_LTE:
- return true; // ~ 10+ Mbps
- case TelephonyManager.NETWORK_TYPE_UNKNOWN:
- return false;
- default:
- return false;
- }
- }
3. Is the access network type 2G, 3G, wap, wifi, etc?
- /**
- * Get the network status, wifi,wap,2g,3g.
- *
- * @param context context
- * @return int Network status {@link#NETWORKTYPE_2G},{@link#NETWORKTYPE_3G}, * {@link#NETWORKTYPE_INVALID},{@link#NETWORKTYPE_WAP}* <p> {@link#NETWORKTYPE_WIFI}
- */
- public static int getNetWorkType(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;
- }
- return mNetWorkType;
- }