WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); assert wifiManager != null; DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); LogUtils.w(dhcpInfo); // ipaddr 172.20.161.205 // gateway 172.20.160.1 // netmask 255.255.254.0 // dns1 172.16.2.15 // dns2 172.16.2.16 // DHCP server 172.20.160.1 // lease 14400 seconds int ip = dhcpInfo.serverAddress; //此处获取ip为整数类型,需要进行转换 final String strIp = intToIp(ip); // 172.20.160.1 ip --->< 27268268 LogUtils.w(strIp + " ip --->< " + ip);
其中用到的方法:
private String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF); } 免责声明:本文内容由凌晨学院网转载自互联网,版权归原作者所有,凌晨学院网不拥有其著作权,亦不承担相应法律责任。如果本站侵犯了您的权益请及时联系team@lcwl.fun,核实后本站将会在2个工作日内对涉嫌侵权内容处理完毕!谢谢合作!