Created
June 20, 2011 14:18
-
-
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.
This file contains 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 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