Android:如何从Android应用程序获取IP地址?

是否有可能从Android应用程序获取IP地址?

我将它保留在我的书签中一段时间​​了,但从未测试过它:

http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

public String getLocalIpAddress() { try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null; } 

如果您使用系绳应用程序进行Android数据连接,请使用系绳连接到PC。 找到ad hoc连接并连接。 然后右键单击并选择状态。 这将为您提供手机的IP地址。 我使用Android应用程序“Barnicle WiFi tether”并使用Windows 7 PC。