Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Created September 29, 2011 13:33
Show Gist options
  • Save stickupkid/1250730 to your computer and use it in GitHub Desktop.
Save stickupkid/1250730 to your computer and use it in GitHub Desktop.
Get local ip address for android
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 (SocketException ex) {
Log.e("SocketException", ex.toString());
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment