【发布时间】:2012-03-07 14:19:56
【问题描述】:
我们正在开发聊天应用程序,我们想使用该代码获取连接到专用网络的模拟器的 IP 地址。我们正在 eclipse 中开发代码。
【问题讨论】:
标签: android
我们正在开发聊天应用程序,我们想使用该代码获取连接到专用网络的模拟器的 IP 地址。我们正在 eclipse 中开发代码。
【问题讨论】:
标签: android
试试这个代码 -
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;
}
看看这个link
【讨论】:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff)
);
来自this thread。不要忘记在您的 AndroidManifest 中拥有 INTERNET 权限!
希望这会有所帮助!
【讨论】: