android获取设备eth0,eth1,wlan0的IP地址
2017-03-16 11:04
1261 查看
在做一些跟网络相关的需求时,很多时候需要获取到相关网络类型的IP地址,如下图:
这个时候用什么方法来获取这个IP地址呢?楼主在之前的开发中,找到了以下方法,代码如下,仅供参考:
感谢阅读,有什么不对的地方还请大家指正。
这个时候用什么方法来获取这个IP地址呢?楼主在之前的开发中,找到了以下方法,代码如下,仅供参考:
/** * Get Ip address 自动获取IP地址 * * @throws SocketException */ public static String getIpAddress(String ipType) throws SocketException { String hostIp = null; try { Enumeration nis = NetworkInterface.getNetworkInterfaces(); InetAddress ia = null; while (nis.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) nis.nextElement(); if (ni.getName().equals(ipType)) { Enumeration<InetAddress> ias = ni.getInetAddresses(); while (ias.hasMoreElements()) { ia = ias.nextElement(); if (ia instanceof Inet6Address) { continue;// skip ipv6 } String ip = ia.getHostAddress(); // 过滤掉127段的ip地址 if (!"127.0.0.1".equals(ip)) { hostIp = ia.getHostAddress(); break; } } } } } catch (SocketException e) { e.printStackTrace(); } Log.d("vivi", "get the IpAddress--> " + hostIp + ""); return hostIp; }其中的ipType就是需要获取的网络ip地址类型,我们可以传入eth1,eth0,wlan0,等,为了正确获取到ip地址,我们需要过滤掉ipv6的地址和127.0.0.1。
感谢阅读,有什么不对的地方还请大家指正。
相关文章推荐
- 根据 设备名(br0/eth0/em0)称获取 当前机器的IP地址与子网掩码信息
- 同wifi环境下android设备自动获取socket服务端的ip地址和端口号的方法-UDP广播
- 如何在android 中获取Wifi设备的IP地址
- android 获取设备信息的IP地址和Mac地址—亲测无误!!
- Android获取移动设备IP地址
- [Android]获取设备IP地址
- Android获取设备ip地址和Mac地址
- Android 获取当前设备的IP地址
- android 获取当前设备的ip地址 mac地址 设备号 手机号 运营商信息
- Android获取局域网所有设备的ip地址
- Android设备信息IP地址等获取
- 如何获取Android设备的IP地址
- Android获取当前设备有效的IP地址
- Android获取移动设备IP地址
- 获取android设备的IP地址
- Android获取设备的IP地址的两种方法
- Android获取移动设备的IP地址
- android:获取本机Mac地址及IP地址方法
- Android获取本机IP地址(不是localhost)和MAC的方法
- Android上获取设备的信息