Code for obtaining the mobile phone IP Address:
[Java]
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;
}
However, in 4.0, an IP address similar to fe80: b607: f9ff: fee5: ipve is displayed. This is an IPV6 address. We need to obtain an IPV4 address, therefore, add a judgment in the appeal code.
[Java]
InetAddressUtils. is%4address (inetAddress. getHostAddress ())
The complete code is as follows:
[Java]
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 () & InetAddressUtils. is00004address (inetAddress. getHostAddress ())){
Return inetAddress. getHostAddress (). toString ();
}
}
}
} Catch (SocketException e ){
// TODO: handle exception
Utils. log ("WifiPreference IpAddress --- error-" + e. toString ());
}
Return null;
}