我想获得WIFI连接使用的IP (不是3G)。有人知道怎么做吗?我使用:
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;}
但它会在ICS上返回3G ip。
谢谢,
亚历克斯
发布于 2012-05-31 22:14:33
您无法检测基于IP地址的连接类型,因为您的移动网络和归属WiFi网络都可以分配私有IP地址。
您需要做的是首先检测您是否有移动网络或WiFi连接,然后根据该信息获取该连接的IP地址。
请在中查看此thread,因此它与您在ICS上的问题相同
发布于 2012-05-31 21:59:13
WifiInfo winfo = ((WifiManager)this.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
winfo.getIpAddress();在执行上述操作之前,您可能需要检查是否已连接到Wifi
https://stackoverflow.com/questions/10835017
复制相似问题