做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 SDK源码提取Python脚本(4.0SDK源码下载地址)
  2. Android省市区3级联动的地址列表的实现
  3. 用cmd 命令更改Android 的默认虚拟机的地址的方法
  4. Android在任何地方模拟点击屏幕
  5. Android获取WIFI状态下的IP地址以及MAC地址
  6. Android Studio 1.0正式稳定版下载地址

随机推荐

  1. HTC XV6850 Android系统移植资源索引
  2. Oprofile分析(android oprofile性能分析)
  3. Windows下搭建Eclipse+Android4.0开发环
  4. [Android]嵌套Fragment以及startActivity
  5. 下载android源码
  6. Android(安卓)为某个App 添加/移除 启动
  7. Android:INSTALL_FAILED_UPDATE_INCOMPATI
  8. Android(安卓)Packaging Problem
  9. listview的滑动删除框架的使用
  10. android中LayoutInflater