3
我试图找到我的网络ip,并使用该ip获取位置。但是,当连接到2G网络时,我的代码会返回正确的IP(223.187.19.157)。但是,当我连接到我的无线网络,它会返回像这样的IP fe80 :: 7ad6:f0ff:fe2b:dca6%wlan0。任何人都可以请帮我找到我的无线上网ip格式正确。代码如下使用wifi ip的位置
public String getIpAddress() {
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) {
ex.printStackTrace();
}
return null;
}
您需要使用WifiManager作为描述在这个所谓的答案](http://stackoverflow.com/questions/7975473/ detect-wifi-ip-address-on-android) – keyser
我和我的中文平板电脑上的这段代码有完全相同的问题。但它在我的SGS手机上可以正常工作。 – deviant