Wednesday, September 22, 2010

Get IP address of device in android

AIM :Get IP address of device in android
Solution: 

The following method returns the device ip address which is currently used by device irrespective of type of connection ie.,either 3g/wifi/any other.

It returns NULL if there is no network connection.


public static String getipAddress() {
            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()) {
                            String ipaddress=inetAddress.getHostAddress().toString();
                            Log.e("ip address",""+ipaddress);
                            return ipaddress;
                        }
                    }
                }
            } catch (SocketException ex) {
                Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
            }
            return null;
    }

Call the above method wherever you wnat to get ip address as
String ipaddress=getipAddress();

--------------------------

18 comments:

  1. check the loop curly bracket that whole loop return false,
    Once it will going to catch Exception then return Null.

    So u need to do Explicitly change IP & get InetAddress API using return IP .getHostAddress();


    if u not get send me Email: linkboy10@yahoo.com

    ReplyDelete
  2. Hi, this is not working for me on android 1.5 :( is there any other solution?

    ReplyDelete
  3. it works on 1.5 you just need to add android.permission.INTERNET to your project's AndroidManifest.xml

    ReplyDelete
  4. The code is working fine on other devices except on Android OS 4.0.3. It returned the fe80::b607:f9ff:fee5:487e..etc … I dont know why.. Please help me

    ReplyDelete
    Replies
    1. public 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()) {
      return inetAddress.getHostAddress().toString();
      }
      }
      }
      } catch (SocketException ex) {
      Log.e(LOG_TAG, ex.toString());
      }
      return null;


      From: http://stackoverflow.com/questions/10835017/android-get-ip-over-wifi-on-ics

      Delete
  5. I am genuinely pleased to read this web site posts which includes tons of useful facts, thanks for providing such
    statistics.
    My web page : sci-fi book blog

    ReplyDelete
  6. I'm really enjoying the theme/design of your website. Do you ever run into any web browser compatibility issues? A number of my blog audience have complained about my site not operating correctly in Explorer but looks great in Safari. Do you have any advice to help fix this issue?
    My homepage : Webmaster Daily Deals

    ReplyDelete
  7. It's awesome to go to see this web site and reading the views of all mates on the topic of this article, while I am also keen of getting experience.
    Here is my homepage :: benefits of digestive enzymes

    ReplyDelete
  8. You should take part in a contest for one of the
    highest quality websites on the internet. I most certainly will highly recommend this blog!



    Here is my web blog i want a tattoo xtranormal

    ReplyDelete
  9. Hi to every , for the reason that I am actually eager of
    reading this blog's post to be updated regularly. It carries good stuff.

    Visit my web page; watch movies on computer

    ReplyDelete
  10. I like the helpful information you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I'm quite certain I will learn many new stuff right here! Good luck for the next!

    Also visit my page secret of lock picking

    ReplyDelete
  11. It's a shame you don't have a donate button! I'd without a doubt donate to this brilliant blog! I guess for now i'll settle
    for bookmarking and adding your RSS feed to my
    Google account. I look forward to new updates and will share this website with
    my Facebook group. Chat soon!

    Here is my web site :: Piano Methods

    ReplyDelete
  12. Does your website have a contact page? I'm having trouble locating it but, I'd like to shoot you an email.

    I've got some creative ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time.

    my site ... Violin Lessons Dvd

    ReplyDelete
  13. Thank you for the good writeup. It in fact was
    a amusement account it. Look advanced to more added agreeable from you!
    However, how could we communicate?

    Have a look at my homepage vocal training Courses

    ReplyDelete
  14. This piece of writing will assist the internet visitors for setting
    up new web site or even a blog from start to end.



    Feel free to visit my weblog - laser tattoo removal cost
    My web site: laser tattoo removal cost

    ReplyDelete
  15. I'm gone to tell my little brother, that he should also pay a quick visit this website on regular basis to take updated from most recent information.

    my page; How Belly Dancing dna

    ReplyDelete
  16. is it possible to get the internet ip address ? (no need for device ip) can u help me to get internet ip (if they are using wifi or 3g or 2g internet or whatever, internet connection using, that internet ip needed)

    ReplyDelete

Android Developers Blog

Ram's shared items