Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created May 7, 2020 08:45
Show Gist options
  • Save vamjakuldip/7a267aa4c454141cd49f7f9d08ea8cca to your computer and use it in GitHub Desktop.
Save vamjakuldip/7a267aa4c454141cd49f7f9d08ea8cca to your computer and use it in GitHub Desktop.
Android studio get local ip address programtically
public static 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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment