How Android determines the type of network (2G,3G or WiFi) _android

Source: Internet
Author: User

This example describes how Android determines the type of network. Share to everyone for your reference, specific as follows:

To determine whether the network type is WiFi, or 3G, or 2G network, different processing of different networks, now will be judged by the method of sorting out for reference

Note: The following data used to move 2G, Unicom 2G, Unicom 3g,wifi I have been tested, the temporary hand does not have a telecom card, so there is no verification, a colleague of the mobile phone, you can verify the results sent to everyone.

Connectivitymanager connectmgr = (Connectivitymanager) this
    . Getsystemservice (context.connectivity_service);
Networkinfo info = Connectmgr.getactivenetworkinfo ();

First, to determine whether the network is WiFi, before the judge must make a non-null judgment, if there is no network connection

Info ==null
info.gettype () = = Connectivitymanager.type_wifi

Second, to determine whether the mobile phone network

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

A detailed distinction between mobile networks:

Info.getsubtype () here uses Getsubtype (), not GetType (), GetType () returns 0, or 1, which distinguishes between a cell phone network or a WiFi

The Info.getsubtype () value list 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's 3G for UMTS or HSDPA, mobile and Unicom 2G for GPRS or EDGE, telecom 2G for CDMA, telecom 3G for 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
    "";
}

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Database Operating skills summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.