简体   繁体   English

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

[英]Getting the IP address of the android device when connected to 3G mobile network

When I am connected to WiFi, I can obtain the IP address of the Android phone.当我连接WiFi时,我可以获得Android手机的IP地址。

However, when on the mobile network like 3G connection, is it still possible to obtain the IP address of the Android phone?但是,在3G等移动网络上,是否还能获取到Android手机的IP地址?
If yes, kindly post the code for the same.如果是,请发布相同的代码。

try something like this尝试这样的事情

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) {}

the mobile device does not have an ip when browsing over 3G connection, You will get the ISP ip on the server side code.移动设备通过 3G 连接浏览时没有 ip,您将在服务器端代码上获得 ISP ip。 I recommend you replace the ip with the unique id, device type and coordinates if possible.如果可能,我建议您将 ip 替换为唯一的 ID、设备类型和坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM