【问题标题】:android emulator ip address安卓模拟器ip地址
【发布时间】:2012-03-07 14:19:56
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个代码 -

    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

    【讨论】:

    • 我们试过了。但我们得到的是 10.0.2.15。这不是我们在模拟器中设置的。我们设置了 192.168.xx.xx。请帮助。提前致谢
    【解决方案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 权限!

    希望这会有所帮助!

    【讨论】:

    • 我们试过了。但我们得到的是 10.0.2.15。这不是我们在模拟器中设置的。我们设置了 192.168.xx.xx。请帮助。提前致谢
    • 我认为这需要 ACCESS_WIFI_STATE 权限。
    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 2011-12-24
    • 2015-03-23
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    相关资源
    最近更新 更多