2013.11.19 ——— android 获取本地ip地址

//获取ip地址    public static String getIpForNet() {          try {              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();                              }                         }                      }                  } catch (SocketException ex) {                      Log.e("WifiPreference IpAddress", ex.toString());                  }                               return null;      }      public static String getIpForWifi(Context context){        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);         WifiInfo wifiInfo = wifiManager.getConnectionInfo();        int ipAddress = wifiInfo.getIpAddress();        String ip = Formatter.formatIpAddress(ipAddress);        return ip;    }



有的时候 可能只获取ipv4的地址,所以 上面的判断需要加上

inetAddress instanceof Inet4Address




更多相关文章

  1. Android SDK和最新ADT下载地址
  2. android 4.0 sdk直接下载地址
  3. android学习路线和环境搭建、推荐一个博客地址
  4. Android Studio下载地址[国内可下载]
  5. Android 底部地址选择弹框
  6. 读取Android设备的MAC地址
  7. Android获取当前WiFi的MAC地址-适配所有版本

随机推荐

  1. 执行javascript作为coldfusion页面加载
  2. 如何将javascript set style属性返回到CS
  3. 带搜索框选择的下拉列表
  4. bootstrap 中 css 与 javascript 的使用
  5. javascript自适应宽度的瀑布流
  6. 如何将占位符放在struts textfield标记中
  7. 使用Acrobat的javascript调试器
  8. PHP发布请求未识别的索引错误
  9. 为什么elementFromPoint()在IE 11上的ifram
  10. JavaScript - 检测click事件是否触发了D