现在的位置: 首页 > 综合 > 正文

android 获取IP

2013年12月10日 ⁄ 综合 ⁄ 共 440字 ⁄ 字号 评论关闭

public String getIP(Context context) {
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 "";
}

抱歉!评论已关闭.