How does android determine the current network type (networking, 2g, 3g, wifi, etc)

Source: Internet
Author: User

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(); returnni != 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 needsNetworkInfoFurther decomposition of information in,NetworkInfoThe 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; }elseif (type.equalsIgnoreCase("MOBILE")) { String proxyHost =; mNetWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP; } }else{ mNetWorkType = NETWORKTYPE_INVALID; } returnmNetWorkType; }

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()) { caseTelephonyManager.NETWORK_TYPE_1xRTT: returnfalse;// ~ 50-100 kbps caseTelephonyManager.NETWORK_TYPE_CDMA: returnfalse;// ~ 14-64 kbps caseTelephonyManager.NETWORK_TYPE_EDGE: returnfalse;// ~ 50-100 kbps caseTelephonyManager.NETWORK_TYPE_EVDO_0: returntrue;// ~ 400-1000 kbps caseTelephonyManager.NETWORK_TYPE_EVDO_A: returntrue;// ~ 600-1400 kbps caseTelephonyManager.NETWORK_TYPE_GPRS: returnfalse;// ~ 100 kbps caseTelephonyManager.NETWORK_TYPE_HSDPA: returntrue;// ~ 2-14 Mbps caseTelephonyManager.NETWORK_TYPE_HSPA: returntrue;// ~ 700-1700 kbps caseTelephonyManager.NETWORK_TYPE_HSUPA: returntrue;// ~ 1-23 Mbps caseTelephonyManager.NETWORK_TYPE_UMTS: returntrue;// ~ 400-7000 kbps caseTelephonyManager.NETWORK_TYPE_EHRPD: returntrue;// ~ 1-2 Mbps caseTelephonyManager.NETWORK_TYPE_EVDO_B: returntrue;// ~ 5 Mbps caseTelephonyManager.NETWORK_TYPE_HSPAP: returntrue;// ~ 10-20 Mbps caseTelephonyManager.NETWORK_TYPE_IDEN: returnfalse;// ~25 kbps caseTelephonyManager.NETWORK_TYPE_LTE: returntrue;// ~ 10+ Mbps caseTelephonyManager.NETWORK_TYPE_UNKNOWN: returnfalse; default: returnfalse; } }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.