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

2023-09-12 07:54:50 作者:、让人潸然泪下的回忆

当我连接到无线网络,我可以得到Android手机的IP地址。

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

然而,当移动网络就像3G连接上,它仍然能获得Android手机的IP地址? 如果是的话,请张贴$ C $下是一样的。

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.

推荐答案

尝试这样的事情

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