发布于 2016-03-06 05:08:35 | 250 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了android实现获取有线和无线Ip地址的方法,较为详细的分析了Android获取IP地址的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android实现获取有线和无线Ip地址的方法。分享给大家供大家参考。具体如下:

做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;
  }
}

分析:

先看这段代码:



intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")


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

再看这段:



 if(!ipaddress.contains("::")){//ipV6的地址


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

希望本文所述对大家的Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务