----转自:http://blog.youkuaiyun.com/android_tutor/article/details/5576544
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
本文提供了一个Java方法用于获取设备的本地IP地址。通过遍历所有网络接口并检查其分配的IP地址来实现这一功能,同时排除了回环地址。

被折叠的 条评论
为什么被折叠?



