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;
}
}
获取本地IP地址
本文提供了一段Java代码,用于从运行中的设备获取本地IP地址。该方法通过枚举所有网络接口并检查每个接口上的IP地址来实现,最终返回非回环地址的有效IP。
1073

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



