Getting IP address of Android when connected to Ce

2019-08-05 09:50发布

问题:

Is there any Simple Way to get the IP address Of my phone when connected to internet through mobile data Network. For getting WiFi IP address i am using following simple Technique.

 WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Is there any way similar to above to get IP address of mobile data network.

I have used following code but it returns MAC addresses ,IP addresses of both WiFi and cellular network but i am interested only in Cellular IP Address.

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();
                                    Log.i("Sarao5",ipAddress);
                                }
                            }
                        }
                    } catch (SocketException ex) {}

回答1:

Use below code as i use in my app -

public static String getDeviceIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface networkInterface : networkInterfaces) {
            List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());
            for (InetAddress inetAddress : inetAddresses) {
                if (!inetAddress.isLoopbackAddress()) {
                    String sAddr = inetAddress.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            // drop ip6 port suffix
                            int delim = sAddr.indexOf('%');
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "";
}

this is best and easy way.

Hope my answer is helpfull.



回答2:

Should avoid using Formatter.formatIPAddress, use the following for getting the IP Address, minor differences from your code; its one function that returns wifi ip if its enabled else the cellular one that you require, you can modify it acc to need;

    public static String getLocalIpAddress() {
            WifiManager wifiMgr = (WifiManager) ApplicationController.getInstance().getSystemService(context.WIFI_SERVICE);
            if(wifiMgr.isWifiEnabled()) {
                WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
                int ip = wifiInfo.getIpAddress();
                String wifiIpAddress = String.format("%d.%d.%d.%d",
                        (ip & 0xff),
                        (ip >> 8 & 0xff),
                        (ip >> 16 & 0xff),
                        (ip >> 24 & 0xff));

                return wifiIpAddress;
            }

            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    Log.i("","111 inetAddress.getHostAddress(): "+inetAddress.getHostAddress());
//the condition after && is missing in your snippet, checking instance of inetAddress
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        Log.i("","111 return inetAddress.getHostAddress(): "+inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();
                    }

                }
            }

        return null;
    }