1 Get IPV4 Address
public string getlocalipaddress () { String address = "0"; try { Enumeration<NetworkInterface> Enumeration = networkinterface.getnetworkinterfaces (); while (Enumeration.hasmoreelements ()) { NetworkInterface networkInterface = Enumeration.nextelement (); enumeration<inetaddress> enumeration2 = networkinterface.getinetaddresses (); while ( Enumeration2.hasmoreelements ()) { inetaddress inetaddress = enumeration2.nextelement (); if (! Inetaddress.isloopbackaddress () && (inetaddress instanceof inet4address)) { address = inetaddress.gethostaddress (). toString (); return address; } } } } catch (exception e) { &NBSP;&NBSP;&NBSP;LOG.E (Tag, e.getmessage (), e); } return address; }
2 Get IPV6 address
public string getlocalipaddress () { String address = "0"; try { Enumeration<NetworkInterface> Enumeration = networkinterface.getnetworkinterfaces (); while (Enumeration.hasmoreelements ()) { NetworkInterface networkInterface = Enumeration.nextelement (); enumeration<inetaddress> enumeration2 = networkinterface.getinetaddresses (); while ( Enumeration2.hasmoreelements ()) { inetaddress inetaddress = enumeration2.nextelement (); if (! Inetaddress.isloopbackaddress ()) { address = Inetaddress.gethostaddress (). toString (); return address; } } } } catch (exception e) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E (Tag, e.getmessage (), e); } return address; }
This article is from the "Jackyanngo" blog, make sure to keep this source http://jackyanngo.blog.51cto.com/5263823/1614612
[Android] Get IP address