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.