3

当我连接到 WiFi 时,我可以获取 Android 手机的 IP 地址。

但是,在3G等移动网络上,是否还能获取到安卓手机的IP地址呢?
如果是,请发布相同的代码。

4

2 回答 2

8

尝试这样的事情

String ipAddress = null;
    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()) {
                    ipAddress = inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {}
于 2011-04-28T16:37:39.090 回答
-1

移动设备通过 3G 连接浏览时没有 ip,您将在服务器端代码上获得 ISP ip。如果可能,我建议您将 ip 替换为唯一 id、设备类型和坐标。

于 2013-12-19T11:41:30.270 回答