Android examples for java.net:InetAddress
get Local InetAddress
import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import java.net.InetAddress; import java.net.UnknownHostException; public class Main{ public static InetAddress getLocalInetAddress(Context context) { int address = getLocalIpAddress(context); if (address == 0) { return null; }//www . j a v a2 s. c o m try { return transform(address); } catch (UnknownHostException e) { e.printStackTrace(); return null; } } public static int getLocalIpAddress(Context context) { WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); return wifiInfo.getIpAddress(); } public static InetAddress transform(int ip) throws UnknownHostException { byte[] arrayOfByte = new byte[4]; arrayOfByte[3] = ((byte) (0xFF & (ip >> 24))); arrayOfByte[2] = ((byte) (0xFF & (ip >> 16))); arrayOfByte[1] = ((byte) (0xFF & (ip >> 8))); arrayOfByte[0] = ((byte) (ip & 0xFF)); InetAddress localInetAddress = InetAddress .getByAddress(arrayOfByte); return localInetAddress; } }