Skip to content

Instantly share code, notes, and snippets.

@paulononaka
Created June 20, 2011 14:18
Show Gist options
  • Save paulononaka/1035679 to your computer and use it in GitHub Desktop.
Save paulononaka/1035679 to your computer and use it in GitHub Desktop.
Get IP address of device in android. The method returns the device ip address which is currently used by device irrespective of type of connection ie.,either 3g/wifi/any other.
public static String getIPAddress() throws SocketException {
for (Enumeration<?> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration<?> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment