To get the phone's IP address code:
public static String getlocalipaddress () {
try{for
(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 e) {
//Todo:handle exception
Utils.log ("Wifipreference ipaddress---error-" + E.tostring ());
}
return null;
}
But in 4.0 there will be a similar fe80::b607:f9ff:fee5:487e IP address, this is the address of IPV6, we need to get the address of the IPV4, so to add a judgment in the appeal code
Inetaddressutils.isipv4address (Inetaddress.gethostaddress ())
The complete code is as follows:
public static String getlocalipaddress () {try{for (ENUMERATION<NETWORKINTERFACE&G T En = Networkinterface.getnetworkinterfaces (); En.hasmoreelements ();)
{NetworkInterface intf = en.nextelement (); for (enumeration<inetaddress> enumipaddr = intf. getinetaddresses (); Enumipaddr.hasmoreel Ements ();)
{InetAddress inetaddress = enumipaddr.nextelement ();
if (!inetaddress.isloopbackaddress () && inetaddressutils.isipv4address (Inetaddress.gethostaddress ())) {
Return inetaddress.gethostaddress (). toString (); }}}catch (SocketException e) {//Todo:handle exception Utils.log ("Wifipreference Ip
Address---error-"+ e.tostring ());
return null; }