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

前端 未结 2 1682
名媛妹妹
名媛妹妹 2020-12-03 13:03

When I am connected to WiFi, I can obtain the IP address of the Android phone.

However, when on the mobile network like 3G connection, is it still possible to obtain

相关标签:
2条回答
  • 2020-12-03 13:43

    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) {}
    
    0 讨论(0)
  • 2020-12-03 13:51

    the mobile device does not have an ip when browsing over 3G connection, You will get the ISP ip on the server side code. I recommend you replace the ip with the unique id, device type and coordinates if possible.

    0 讨论(0)
提交回复
热议问题