The Android network-------------------determine the status of the networks (network connection, change, and judgment 2g/3g/4g)

Source: Internet
Author: User



Most apps now need to get data from the Web. So access to the network is inevitable. But before we go back to the network, we should make a state judgment of the network first. In fact, before accessing the network we have to do some state judgment, corresponding to some state judgment to do processing, not directly using HTTP to access the network. Many people in the development of the network is often the block directly skip, direct access to the network, can be a broken network, a variety of experience effect is not good, not to say that the app can not be used, just experience poor results. Also, we may consider for users, because now the general network is wifi and mobile phone traffic, are aware that the latter charge is relatively high. If our app loaded images or have large data download operation, but the user's intention is that if the traffic under the words do not operate these very cost-of-flow operation, so we have to do some of the network state of judgment. Whether the network is connected well, connected WiFi or traffic, broken network or network changed the time to do, this is some details, but pay attention to processing.

We have to make a judgment on whether to connect the Internet when we do the interview. Judging the network is also relatively simple, the use of two of classes. Connectivitymanager and Networkinfo.

Just 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 there is a connection is a net, Networkinfo is the information of the network. There is also a simpler way to judge whether a network is connected.

Connectivitymanager connmgr = (connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Connmgr.getactivenetworkinfo ();

In one is to listen to network changes, is to set up a broadcast.

Register for the broadcast, listen connectivitymanager.connectivity_action this action

Intentfilter filter = new Intentfilter (connectivitymanager.connectivity_action); receiver = new Networkreceiver (); Context.registerreceiver (receiver, filter);


A broadcast class can be implemented again. So the network changes, it will be broadcast, and then perform the corresponding operation.

public class Networkreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {L OG.I ("TAG", "intent============>>>>" + intent.tostring ());}}


Here's a look at the type of cell phone network, 2g,3g,4g

About the network type, after getting networkinfo = Connmgr.getnetworkinfo (connectivitymanager.type_mobile); After this, OK is the cellular network, The type of network is obtained through Networkinfo.getsubtype (), and the network name is obtained by using networkinfo.getsubtypename ().

We use this networkinfo.getsubtype () type to determine what the current network is.

In fact, the Android API has defined a variety of network states, not within the Connectivitymanager definition, but in the telephonymanager inside the definition. With the modification of different API versions, there are currently 14 status values added:

The following is through their own data, and look at other people to check the data obtained from the experiment. Tidy up as follows:

--------------------Added in API level 1---------------------public static final int network_type_unknownnetwork TYPE is Unknownconstant value:0 (0x00000000) (not known network type) public static final int network_type_gprscurrent network is gprsconstant Va Lue:1 (0x00000001) (2.5G) mobile and unicom public static final int network_type_edgecurrent NETWORK is edgeconstant value:2 (0x0000000 2) (2.75G) 2.5G to 3G transition Mobile and 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 telco) public static final int Network_type_evdo_0curr ENT Network is EVDO revision 0Constant value:5 (0x00000005) (3G) telecommunications public static final int network_type_evdo_acurrent net Work is EVDO revision aconstant value:6 (0x00000006) (3.5G) belongs to 3G transition public static final int network_type_1xrttcurrent Networ K is 1xRTTConstant Value:7 (0x00000007) (2G)---------------------Added in API level 5--------------------public static final int Network_type_ Hsdpacurrent Network is hsdpaconstant value:8 (0x00000008) (3.5G) public static final int network_type_hsupacurrent Networ K is hsupaconstant Value:9 (0x00000009) (3.5G) public static final int network_type_hspacurrent NETWORK is hspaconstant V Alue:10 (0x0000000a) (3G) Unicom--------------------------Added in API level 8-------------------------public static final I NT Network_type_idencurrent NETWORK is idenconstant value:11 (0x0000000b) (2G)--------------------------Added in API Lev  El 9-------------------------public static final int network_type_evdo_bcurrent NETWORK is EVDO revision bconstant Value: (0x0000000c) 3g-3.5g--------------------------Added in API level one------------------------public static final int Network_type_ltecurrent Network is lteconstant value:13 (0x0000000d) (4G) public static final int Network_type_ Ehrpdcurrent Network is ehrpdconstant value:14 (0x0000000e) 3G (3G to 4G upgrade product)--------------------------Added in API level---------------------------public static final int Network_type_hspapcurrent Network is hspa+constant value:15 (0x0000000f) (3G)

So we can networkinfo.getsubtype () to get the value and the above to correspond, we know that the current is a few G network. Because of the mobile phone card is limited, so some not all, welcome to add, share.


The Android network-------------------determine the status of the networks (network connection, change, and judgment 2g/3g/4g)

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.