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