Android Network-determine the Network status (Network connection, change, and determine 2G/3G/4G)

Source: Internet
Author: User

Android Network-determine the Network status (Network connection, change, and determine 2G/3G/4G)



Currently, most apps need to obtain data from the network. Therefore, it is inevitable to access the network. Before accessing the network, we should first determine the network status. In fact, before accessing the network, we need to make some status judgments and corresponding status judgments for processing, instead of directly accessing the network through Http. During development, many people often skip the network and directly access the network. The network can be disconnected. The effects of various experiences are not good. It is not to say that the app cannot be used, but the experience is poor. In addition, we may consider it for users, because the current network connection is generally wifi and mobile phone traffic, and we all know that the latter is relatively expensive. If our app loads images or has large data downloading operations, but the user's intention is not to perform these expensive operations under the traffic, in this way, we must make some judgment on the network connection status. Whether the network is connected well, whether it is connected to wifi or traffic, how to do it when the network is disconnected or the network is changed, this is some details, but pay attention to the processing.

 

We have to determine whether the network is connected during access. It is also easy to judge the connection, and two classes are used. ConnectivityManager and NetworkInfo.

 

You only need to execute the following code.

ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);boolean isWifiConn = networkInfo.isConnected();networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);boolean isMobileConn = networkInfo.isConnected();

 

As long as a connection has a network, NetworkInfo is the network information. There is also a simpler way to determine whether the network is connected.

ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

 

One is listening for network changes, or setting a broadcast.

Register the broadcast and listen for the ConnectivityManager. CONNECTIVITY_ACTION action.

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);receiver = new NetworkReceiver();context.registerReceiver(receiver, filter);

 

You can implement another broadcast class. As soon as the network changes, it will be broadcast and then perform corresponding operations.

public class NetworkReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Log.i("TAG", "intent============>>>>" + intent.toString());}}


Next, let's take a look at how to determine the type of the mobile phone network, 2G, 3G, 4G

For the network type, obtain networkInfo = connMgr. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); after confirming that it is a mobile phone network, it is passed through networkInfo. getSubtype () will get the network type, with networkInfo. getSubtypeName () will get the network name.

The networkInfo. getSubtype () is used to determine the network type.

In fact, the Android Api has defined various network statuses, not in ConnectivityManager, but in TelephonyManager. By modifying different api versions, the current status value has been increased to 14:

The following is the result of self-checking information and reading other people's materials. As follows:

 

Incluadded in API level 1 extends public static final int NETWORK_TYPE_UNKNOWNNetwork type is unknownConstant Value: 0 (0x00000000) (do not know the network type) public static final networkint _type_gprs scurrent network is uplsconstant Value: 1 (0x00000001) (2.5G) China Mobile and China Unicom public static final int NETWORK_TYPE_EDGECurrent network is EDGEConstant Value: 2 (0x00000002) (2.75G) 2G to 3G transitional Mobile and China Unicom public static final int NETWORK_TYPE_UMTSCurrent network is UMTSConstant Value: 3 (0x00000003) (3G) unicom ----------------- Added in API level 4 ------------------- public static final int NETWORK_TYPE_CDMACurrent network is CDMA: Either IS95A or IS95BConstant Value: 4 (0x00000004) (2G Telecom) public static final int NETWORK_TYPE_EVDO_0Current network is EVDO revision 0 Constant Value: 5 (0x00000005) (3G) china telecom public static final int NETWORK_TYPE_EVDO_ACurrent network is EVDO revision AConstant Value: 6 (0x00000006) (3.5G) belongs to the 3G transition public static final int NETWORK_TYPE_1xRTTCurrent network is 1 xRTTConstant Value: 7 (0x00000007) (2G) incluadded in API level 5 ------------------ public static final int NETWORK_TYPE_HSDPACurrent network is HSDPAConstant Value: 8 (0x00000008) (3.5G) public static final int nation network is HSUPAConstant Value: 9 (0x00000009) (3.5G) public static final int NETWORK_TYPE_HSPACurrent network is HSPAConstant Value: 10 (0x0000000a) (3G) unicom -------------------------- Added in API level 8 ------------------------- public static final int NETWORK_TYPE_IDENCurrent network is iDenConstant Value: 11 (0x0000000b) (2G) -------------------------- Added in API level 9 ------------------------- public static final int NETWORK_TYPE_EVDO_BCurrent network is EVDO revision BConstant Value: 12 (0x0000000c) 3g-3.5G memory Added in API level 11 memory public static final int NETWORK_TYPE_LTECurrent network is LTEConstant Value: 13 (0x0000000d) (4G) public static final int destination network is eHRPDConstant Value: 14 (0x0000000e) 3G (3G to 4G upgrade product) ---------------------- Added in API level 13 implements public static final int NETWORK_TYPE_HSPAPCurrent network is HSPA + Constant Value: 15 (0x0000000f) (3G)

 

So we can get the value of networkInfo. getSubtype () to correspond to the above, and we will know the current number of gnetwork. Due to the limited number of mobile phone cards in your hand, you are welcome to add and share them.

 

 

 

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: info-contact@alibabacloud.com 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.