Android implementation to obtain wired and wireless IP address method _android

Source: Internet
Author: User

This article describes the Android implementation approach to acquiring wired and wireless IP addresses. Share to everyone for your reference. Specifically as follows:

To do the development of Android, encountered the problem of getting a wired IP address. Not much to say code!

for (enumeration<networkinterface> en = NetworkInterface
   . getnetworkinterfaces (); en.hasmoreelements ();) {
  NetworkInterface intf = En.nextelement ();
  if (Intf.getname (). toLowerCase (). Equals ("eth0") | | intf.getname (). toLowerCase (). Equals ("Wlan0")) {for 
   ( enumeration<inetaddress> enumipaddr = intf.getinetaddresses (); Enumipaddr.hasmoreelements ();) {
   InetAddress inetaddress = Enumipaddr.nextelement ();
   if (!inetaddress.isloopbackaddress ()) {
    IPAddress = inetaddress.gethostaddress (). toString ();
    if (!ipaddress.contains ("::")) {//ipv6 address return
    IPAddress}}
   }
   }
  else {
   continue;
  }
}

Analysis:

Let's look at this piece of code:

Copy Code code as follows:

Intf.getname (). toLowerCase (). Equals ("eth0") | | Intf.getname (). toLowerCase (). Equals ("Wlan0")

This means that only wireless and wired IP are filtered. NetworkInterface is a lot of names such as SIM0,REMT1 ... I don't need it, I just filter it.

Look at this paragraph again:

Copy Code code as follows:

if (!ipaddress.contains ("::")) {//ipv6 address

It says here: Filter out the address of the IPv6. This address is available either wirelessly or wired, and my address is generally: fe80::288:88ff:fe00:1%eth0 fe80::ee17:2fff:fece:c0b4%wlan0 are generally present in the first cycle. The second cycle is the real IPv4 address.

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

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.