0

I'm using this to get the internal IP when connected to Wi-Fi:

            WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();

            int ipv4intAddress = wifiInfo.getIpAddress();
            tempView = (TextView) findViewById(R.id.textViewIPv4Int2);
            tempView.setText(myNetworkOperations.iptoDecimal(ipv4intAddress));

Is there a similar simple way to get that IP when on mobile or other type of connection?

  • possible duplicate of [How to get IP address of the device?](http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device) – Marcus Feb 25 '15 at 19:01

1 Answers1

1

You can try with this code:

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()) {
                return inetAddress.getHostAddress().toString();
            }
        }
    }
} catch (SocketException ex) {
    return "ERROR Obtaining IP";
}
return "No IP Available";
jlopez
  • 6,287
  • 2
  • 51
  • 90