Finding IP address of your Android device programatically

 You can find more about finding IP address of Android device programatically



public String getLocalIpAddress()
  {
          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 (Exception ex) {
              Log.e("IP Address", ex.toString());
          }
          return null;
      }

Add below permission in the manifest file.
   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Happy coding :-)

16 comments:

  1. I am not getting the required IP address.. I am staying India & its showing canada ip address.. what might be the problem.I am using same code.. working fine but showing wrong IP address.

    ReplyDelete
    Replies
    1. Ok! You mayn't find the exact location of IP network provider.

      Delete
    2. OK! You may not find the exact location of IP network provider address for solving your service grievances, however you search the IP address at IP-Details.com for the required IP details.

      Delete
  2. Thank you very much :)

    ReplyDelete
  3. This is not correct. Please don't use this code.

    ReplyDelete
  4. Where to write this code? Is there any emulator for android ?

    ReplyDelete
  5. instead of
    if (!inetAddress.isLoopbackAddress()) {

    use
    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {

    for getting ipv4 address

    ReplyDelete
  6. not correct, bad code!
    it may work in some scenario.
    better list all IP addresses than give just the first not equal to 127.0.0.1

    ReplyDelete
  7. Here i am posting complete solution for getting IP Address for any type of network

    public static String getLocalIpAddress() {
    try {
    for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
    NetworkInterface intf = en.nextElement();
    for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
    InetAddress inetAddress = enumIpAddr.nextElement();
    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
    return inetAddress.getHostAddress().toString();
    }
    }
    }

    } catch (Exception ex) {

    }
    return null;
    }

    Thanks & Regards
    Manoj
    +91-8553540873
    manoj.cs.iu@gmail.com

    ReplyDelete
    Replies
    1. I got error in "InetAddressUtils". How can I solve it. Android Studio suggest me to create class. What's the solution?

      Thanks.

      Delete
  8. i want the external ip address if the user is connected to wifi.

    ReplyDelete