获取手机本地IP地址

时间:2025-03-26 12:36:37
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) {
}
return null;
}