Android Get IP Address

Source: Internet
Author: User
Tags get ip

Original: http://www.cnblogs.com/android100/p/Android-get-ip.html

1. Use WiFi

Set user Permissions First

  1. <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE"></ Uses-permission>
  2. <uses-permission android:name="Android.permission.CHANGE_WIFI_STATE"></ Uses-permission>
  3. <uses-permission android:name="Android.permission.WAKE_LOCK"></uses-permission >

Second, the code is as follows

  1. Public void OnCreate (Bundle savedinstancestate) {
  2. super.oncreate (savedinstancestate);
  3. Setcontentview (R.layout.main);
  4. //Get WiFi service
  5. Wifimanager Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
  6. //Determine if WiFi is turned on
  7. if (!wifimanager.iswifienabled ()) {
  8. Wifimanager.setwifienabled (true);
  9. }
  10. Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();
  11. int ipAddress = wifiinfo.getipaddress ();
  12. String IP = Inttoip (ipAddress);
  13. EditText et = (EditText) Findviewbyid (r.id.edittext01);
  14. Et.settext (IP);
  15. }
  16. private String Inttoip (int i) {
  17. return (I & 0xFF) + "." +
  18. ((I >> 8) & 0xFF) + "." +
  19. ((I >> ) & 0xFF) + "." +
  20. (I >> & 0xFF);
  21. }

2. Using GPRS

First, set the user's Internet access rights

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

Second, the code is as follows

  1. Public String getlocalipaddress ()
  2. {
  3. Try
  4. {
  5. For (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces (); en.hasmoreelements ();)
  6. {
  7. NetworkInterface intf = En.nextelement ();
  8. For (enumeration<inetaddress> enumipaddr = intf.getinetaddresses (); enumipaddr.hasmoreelements ();)
  9. {
  10. InetAddress inetaddress = Enumipaddr.nextelement ();
  11. if (!inetaddress.isloopbackaddress ())
  12. {
  13. return Inetaddress.gethostaddress (). toString ();
  14. }
  15. }
  16. }
  17. }
  18. catch (SocketException ex)
  19. {
  20. LOG.E ("Wifipreference IpAddress", ex.tostring ());
  21. }
  22. return null;
  23. }

Android Get IP Address

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.