Android Network type judgment (2g, 3g, WiFi)

Source: Internet
Author: User

Determine whether the network type is WiFi, 3G, or 2G network, for different

Network for different processing, now the judgment method is collated to everyone, for reference

Note: The data used below move 2G, Unicom 2G, Unicom 3g,wifi I have already tested, temporarily hand

There is no telecom card, so there is no authentication, a colleague with a telecom phone, can verify the results after verification

Sent to everyone.

Connectivitymanager connectmgr = (Connectivitymanager) This

. Getsystemservice (Context.connectivity_service);

Networkinfo info = Connectmgr.getactivenetworkinfo ();

First, determine whether the network is WiFi, before judging the non-null judgment must be carried out, if there is no network

Connect Info ==null

Info.gettype () = = Connectivitymanager.type_wifi

Second, to determine whether the mobile network

Info!=null && info.gettype () = = Connectivitymanager.type_mobile

The cellular network makes a detailed distinction:

Info.getsubtype () here uses Getsubtype (), not GetType (), GetType () returns the

is 0, or 1, is a cell phone network or WiFi

The list of Info.getsubtype () values is as follows:

* NETWORK_TYPE_CDMA network type is CDMA

* Network_type_edge network type is EDGE

* Network_type_evdo_0 network type is EVDO0

* Network_type_evdo_a network type is EVDOA

* Network_type_gprs network type is GPRS

* NETWORK_TYPE_HSDPA network type is HSDPA

* Network_type_hspa network type is HSPA

* Network_type_hsupa network type is HSUPA

* Network_type_umts network type is UMTS

Unicom 3G for UMTS or HSDPA, mobile and Unicom 2G for GPRS or edge, telecommunications 2G for CDMA, telecommunications

The 3G is EVDO

Android Gets the IP address of the phone

Private string getphoneip ()  {        try {               for  (enumeration< Networkinterface> en = networkinterface.getnetworkinterfaces ();  en.hasMoreElements ();)  {                   networkinterface intf = en.nextelement ();                   for  (enumeration<inetaddress>  Enumipaddr = intf.getinetaddresses ();  enumipaddr.hasmoreelements ();)  {                        inetaddress inetaddress = enumipaddr.nextelement ();                       if  (!inetaddress.isloopbackaddress ()  && inetaddress instanceof inet4address)  {                       //if   (!inetaddress.isloopbackaddress ()  && inetaddress instanceof inet6address)  {                           return inetaddress.gethostaddress (). toString ();                        }                   }              }           } catch  (exception e)  {           }          return  "";      }

Android Network type judgment (2g, 3g, WiFi)

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.