连接到3G移动网络时获取Android设备的IP地址

时间:2011-04-28 12:39:37

标签: java android ip-address 3g-network

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

但是,在3G连接等移动网络上,是否仍然可以获取Android手机的IP地址? 如果是,请发布相同的代码。

2 个答案:

答案 0 :(得分: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) {}

答案 1 :(得分:-1)

移动设备在浏览3G连接时没有ip,您将获得服务器端的ISP IP代码。我建议你用唯一的ID,设备类型和坐标代替ip。