Android-获取IP

【Android-获取IP】人生难得几回搏,此时不搏待何时。这篇文章主要讲述Android-获取IP相关的知识,希望能为你提供帮助。

依赖

compile files(‘libs/httpclient-4.5.2.jar‘)

源码
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()& & InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
{
return inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex)
{
Log.e("WifiPreferenceIpAddress", ex.toString());
}
return null;
}


    推荐阅读