Android access to IP address implementation method _android

Source: Internet
Author: User
Tags get ip

Android Get IP Address

Recently done project, there is a demand is the Android device to obtain the current IP function, after a query to solve the data, record the implementation method.

1. Using WiFi

Set user Permissions First

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>

< Uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission>

< Uses-permission android:name= "Android.permission.WAKE_LOCK" ></uses-permission>

Second, the code is as follows

public void OnCreate (Bundle savedinstancestate) {

    super.oncreate (savedinstancestate);

    Setcontentview (r.layout.main);

    

    Get WIFI service

    wifimanager Wifimanager = (wifimanager) getsystemservice (context.wifi_service);

    Determine if WiFi is open if

    (!wifimanager.iswifienabled ()) {

    wifimanager.setwifienabled (true); 

    }

    Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();   

    int ipaddress = wifiinfo.getipaddress (); 

    String IP = Inttoip (ipaddress); 

    EditText et = (edittext) Findviewbyid (r.id.edittext01);

    Et.settext (IP);

  }  

  Private String Inttoip (int i) {return   

    

     (I & 0xFF) + "." +   

    ((I >> 8) & 0xFF) + "." + (   

    (i) ;> & 0xFF) + "." +   

    (I >> & 0xFF);

   } 

2. Using GPRS

First, set the user access rights

<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission>

Second, the code is as follows

Public String getlocalipaddress ()

  {

    try

    {

      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 ())

          {return

            inetaddress.gethostaddress (). toString ();

    }

    catch (SocketException ex)

    {

      log.e ("Wifipreference IPAddress", ex.tostring ());

    }

    return null;

  }

Thank you for reading, I hope to help you, thank you for your support for this site!

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.