本文将带你了解Android应用开发之获取当前手机的 DHCP 信息ip,server 等操作实例教程,希望本文对大家学Android有所帮助。
android 获取当前手机的 DHCP 信息ip,server 等操作实例教程
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 secondsint ip = dhcpInfo.serverAddress;//此处获取ip为整数类型,需要进行转换final String strIp = intToIp(ip); // 172.20.160.1 ip --->< 27268268LogUtils.w(strIp + " ip --->< " + ip);
其中用到的方法:
?1234private String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."+ ((i >> 24) & 0xFF);}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!