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

南笙酒味 提交于 2019-11-26 16:48:15

问题


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 the IP address of the Android phone?
If yes, kindly post the code for the same.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5818725/getting-the-ip-address-of-the-android-device-when-connected-to-3g-mobile-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!