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

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

for (Enumeration en = NetworkInterface   .getNetworkInterfaces(); en.hasMoreElements();) {  NetworkInterface intf = en.nextElement();  if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) {    for (Enumeration 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程序设计有所帮助。

更多相关文章

  1. Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上
  2. Android使用okhttp框架实现带参数Get和Post请求(附服务端完整代码
  3. Android SDK下载地址
  4. Android ListView 滚动条的设置详解及实例代码
  5. Android NDK c调用java代码
  6. Android 代码风格指南

随机推荐

  1. android:Spinner(下拉框)控件的使用
  2. android 自定义View 在布局文件中写法
  3. Visual Studio Magazine -Mono for Andro
  4. Android(安卓)一个apk多个入口
  5. Android上传下载文件(图片)
  6. android EditText设置弹出数字输入法键盘
  7. Android中的Junit单元测试
  8. Android-statuabar电池管理
  9. android 列表 数据显示总结
  10. Android(安卓)Studio Build running 时间