android模拟器的ip地址

时间:2012-02-16 07:10:29

标签: android

我们正在开发聊天应用程序,我们想要使用代码获取连接到专用网络的仿真器的IP地址。我们正在eclipse中开发代码。

2 个答案:

答案 0 :(得分:2)

试试这段代码 -

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

答案 1 :(得分:2)

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权限!

希望这有帮助!