private String getLocalIPAddress() throws SocketException{      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())){                  return inetAddress.getHostAddress().toString();              }          }      }      return "null";  }

但是,在android4.0以上系统中,上面的代码仅能够返回一个ipv6的地址,如果需要获取ip v4的地址,可以这么更改:

private String getLocalIPAddress() throws SocketException{      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() && <strong>(inetAddress instanceof Inet4Address)</strong>){                  return inetAddress.getHostAddress().toString();              }          }      }      return "null";  }






更多相关文章

  1. 36 Android(安卓)Activity跳转动画 获取当前版本
  2. Android中简单的发短信应用
  3. EditText获取焦点的一系列方法
  4. Android获取手机屏幕参数的工具
  5. android中获取手机短信,删除短信功能
  6. BigImageViewer
  7. 跨进程存取数据
  8. android 下的内存状态获取
  9. 【Android】问题记录

随机推荐

  1. 关于android中的.9图(一)——如何画.9图
  2. android 五子棋实验总结
  3. Android拷贝工程不覆盖原工程的配置方法
  4. 请介绍下Android中常用的五种布局
  5. 《Android传感器高级编程》
  6. Android 高级UI组件
  7. Android找不到Manifest系统文件
  8. Android Spinner自动弹出列表,设置title
  9. J2me游戏如何快速移植到Android(安卓)(2)
  10. 浅谈android中图片处理之图形变换特效Mat