Created
May 7, 2020 08:45
-
-
Save vamjakuldip/7a267aa4c454141cd49f7f9d08ea8cca to your computer and use it in GitHub Desktop.
Android studio get local ip address programtically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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