大话西游666
我使用了以下代码:我之所以使用hashCode是因为当我使用getHostAddress..但hashCode对我来说工作得很好,因为我可以使用格式化程序获得具有正确格式的IP地址。下面是示例输出:1.使用getHostAddress : ***** IP=fe80::65ca:a13d:ea5a:233d%rmnet_sdio02.使用hashCode和Formatter : ***** IP=238.194.77.212正如你所看到的,第二种方法正好给了我所需要的。public 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()) {
String ip = Formatter.formatIpAddress(inetAddress.hashCode());
Log.i(TAG, "***** IP="+ ip);
return ip;
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;}