Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save codeswimmer/977908 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/977908 to your computer and use it in GitHub Desktop.
Android: NetworkUtil.determineDeviceIpAddress()
public static final String determineDeviceIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
if (en != null) {
for (; 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 e) {
Log.w(TAG, String.format("determineDeviceIpAddress() - %s", e.toString()));
}
return Values.EMPTY_STRING;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment