Created
May 18, 2011 02:52
-
-
Save codeswimmer/977908 to your computer and use it in GitHub Desktop.
Android: NetworkUtil.determineDeviceIpAddress()
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 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