Android(安卓)IPV6获取方式
16lz
2021-01-25
下面的代码中我做了校验真实的ipv6获取ipv6去除%后面的值然后通过冒号校验位数,然后截取第一段判断是否包括fe或者fc包括就表示假的
//ipv6 public static String getLocalIpV6() { try { for (Enumeration en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); // logger.error("ip1 " + inetAddress); logger.error("ip1 " + inetAddress.getHostAddress()); /* logger.error("getHostName " + inetAddress.getHostName()); logger.error("getCanonicalHostName " + inetAddress.getCanonicalHostName()); logger.error("getAddress " + Arrays.toString(inetAddress.getAddress())); logger.error("getHostAddress " + inetAddress.getHostAddress());*/ if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) { return inetAddress.getHostAddress(); } } } } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; } public static String getlocalIp() { String ip; try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {// ip=inetAddress.getHostAddress().toString(); System.out.println("ip==========" + inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } catch (SocketException ignored) { } return null; } public static String validateV6() { new Thread(new Runnable() { @Override public void run() { hostIp6 = getLocalIpV6(); } }).start(); //过滤找到真实的ipv6地址 logger.error("v6 validateV6 " + hostIp6); if (hostIp6 != null && hostIp6.contains("%")) { String[] split = hostIp6.split("%"); String s1 = split[0]; logger.error("v6 remove % is " + s1); if (s1 != null && s1.contains(":")) { String[] split1 = s1.split(":"); if (split1.length == 6||split1.length==8) { if (split1[0].contains("fe") || split1[0].contains("fc")) { return "0.0.0.0"; } else { return s1; } } } } return "0.0.0.0"; }
欢迎加入技术分享学习公众号【码兄】
更多相关文章
- Android(安卓)string.xml 显示特殊符号
- Android框架之路——ToolBar的使用
- Android---配置adb环境变量
- Android微信分享---点击分享伤害了我且一闪而过~
- Vue实现掘金导航栏效果
- Vue实现简书导航栏效果
- android号码匹配位的修改
- android在代码里设置button或者textView的字体颜色
- 2017年11月1日Android职位数据分析