How does android determine the current network type (networking, 2g, 3g, wifi, etc)
Generally, we determine whether the network is connected. However, sometimes we need to know the specific network type. For example, in a 3G network, the positioning function of Baidu map is very different, in this case, you need to determine the network type in advance for special handling of 3G network conditions.
There are a lot of online code about how Android detects network types, but there are not many useful ones. I only find one article to solve the problem (see the URL at the end of the article ), in order to better facilitate your understanding, I have explained and sorted out this article.
In android, ConnectivityManager is used to determine the network. The following method demonstrates how to use ConnectivityManager to determine whether the network is connected:
12345 |
public static boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) AppContext.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); return ni != null && ni.isConnectedOrConnecting(); } |
There is a question,isConnectedOrConnecting()
Whether it is a connection or a connection. If the connection is an unavailable network, it should not be connected. So I think it is better to change it to isConnected, according to your individual needs.
The above is just to determine whether the network is connected, there is a big gap with our purpose, in order to obtain a specific network type needsNetworkInfo
Further decomposition of information in,NetworkInfo
The getTypeName () method of can be used to determine whether it is a Wi-Fi or mobile network. The getType method corresponds to the getTypeName () method, and the return value is an integer constant representing the connection type.
ConnectivityManager. TYPE_MOBILE,
ConnectivityManager. TYPE_WIFI,
ConnectivityManager. TYPE_WIMAX,
ConnectivityManager. TYPE_ETHERNET,
ConnectivityManager. TYPE_BLUETOOTH
Among the five types, only ConnectivityManager. TYPE_MOBILE and ConnectivityManager. TYPE_WIFI are related to the Internet. It can be seen that ConnectivityManager is not dedicated to managing Internet connections, but also responsible for Bluetooth and other connections.
First, define some Integer Variables to represent different networks:
12345678910 |
/** 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 and above networks, or collectively referred to as a fast Network */ public static final int NETWORKTYPE_3G = 3; /** Wifi network */ public static final int NETWORKTYPE_WIFI = 4; |
These constants will be used later.
Next, we can use a method to determine whether it is wifi or mobile network. If it is mobile network, we can further analyze whether it is 3g or 2g through other methods:
123456789101112131415161718192021222324 |
/** * Obtain the network status, wifi, wap, 2g, 3g. * * @ Param context * @ Return int network status {@ link # NETWORKTYPE_2G}, {@ link # NETWORKTYPE_3G}, * {@ link # NETWORKTYPE_INVALID}, {@ link # NETWORKTYPE_WAP }* {@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; }
|
We have defined the difference between 3G and 2g.isFastMobileNetwork(context)
Method, mainly usedTelephonyManager
:
123456789101112131415161718192021222324252627282930313233343536373839 |
private static boolean isFastMobileNetwork(Context context) { TelephonyManager 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 ; } } |
The whole process can be summarized as follows: the first step is to use ConnectivityManager to determine whether it is a Wi-Fi or mobile network, and use TelephonyManager to determine the mobile network type.