做android的开发时,遇到了获取有线ip地址的问题.不多说 上代码!

for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipaddress = inetAddress.getHostAddress().toString();
if(!ipaddress.contains("::")){//ipV6的地址
return ipaddress;
}
}
}
} else {
continue;
}
}

红色地方表示:仅过滤无线和有线的ip. networkInterface是有很多的名称的 比如sim0,remt1.....等等.我不需要用到就直接过滤了

绿色的地方表示: 过滤掉ipv6的地址.不管无线还是有线 都有这个地址, 我这边显示地址大体是:fe80::288:88ff:fe00:1%eth0 fe80::ee17:2fff:fece:c0b4%wlan0 一般都是出现在第一次循环.第二次循环就是真正的ipv4的地址.

更多相关文章

  1. Android获取本机Mac地址及IP地址方法
  2. Android(安卓)SDK源码提取Python脚本(4.0SDK源码下载地址)
  3. Android(安卓)NDK中使用LOG调试C++程序
  4. Android(安卓)微博登录
  5. Google 告诉你 Android(安卓)4.0 的新功能 [视频]
  6. Android省市区3级联动的地址列表的实现
  7. android IP getIP
  8. Android硬件开发之——NFC技术
  9. Android优秀开源项目收集

随机推荐

  1. Android(安卓)应用程序签名
  2. 如何在android 文件选择器中选择多个图片
  3. android使用html开发软件界面
  4. 修正Android摄像头API
  5. Android沉浸式状态栏最省事的解决方案
  6. [Android1.5]DigitalClock自定义日期输出
  7. 【android开发】Android(安卓)2.2版本的
  8. Android开发 数据库操作
  9. Android高手进阶教程(二十五)之---Androi
  10. 致Android初学者:如何学习Android开发