get Local Ip address in String value - Android java.net

Android examples for java.net:IP Address

Description

get Local Ip address in String value

Demo Code

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class Main {

  public static String getLocalIpString(Context context) {
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (!wm.isWifiEnabled()) {
      return null;
    }/*  ww w. j  ava  2s  . c  o  m*/

    WifiInfo wi = wm.getConnectionInfo();
    return intToString(wi.getIpAddress());
  }

  private static String intToString(int i) {
    return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
  }

}

Related Tutorials