获取wifi的ip,子网掩码,网关,dns之类的信息
2016-12-23 14:02
701 查看
public class MainActivity extends Activity { private WifiManager my_wifiManager; private WifiInfo wifiInfo; private DhcpInfo dhcpInfo; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); my_wifiManager = ((WifiManager) getSystemService("wifi")); dhcpInfo = my_wifiManager.getDhcpInfo(); wifiInfo = my_wifiManager.getConnectionInfo(); tvResult = (TextView) findViewById(R.id.tvResult); } @Override protected void onResume() { super.onResume(); StringBuilder sb = new StringBuilder(); sb.append("网络信息:"); sb.append("\nipAddress:" + intToIp(dhcpInfo.ipAddress)); sb.append("\nnetmask:" + intToIp(dhcpInfo.netmask)); sb.append("\ngateway:" + intToIp(dhcpInfo.gateway)); sb.append("\nserverAddress:" + intToIp(dhcpInfo.serverAddress)); sb.append("\ndns1:" + intToIp(dhcpInfo.dns1)); sb.append("\ndns2:" + intToIp(dhcpInfo.dns2)); sb.append("\n"); System.out.println(intToIp(dhcpInfo.ipAddress)); System.out.println(intToIp(dhcpInfo.netmask)); System.out.println(intToIp(dhcpInfo.gateway)); System.out.println(intToIp(dhcpInfo.serverAddress)); System.out.println(intToIp(dhcpInfo.dns1)); System.out.println(intToIp(dhcpInfo.dns2)); System.out.println(dhcpInfo.leaseDuration); sb.append("Wifi信息:"); sb.append("\nIpAddress:" + intToIp(wifiInfo.getIpAddress())); sb.append("\nMacAddress:" + wifiInfo.getMacAddress()); tvResult.setText(sb.toString()); } private String intToIp(int paramInt) { return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "." + (0xFF & paramInt >> 24); } }
效果如下:
备注:
android.net.NetworkUtils
/** * Convert a IPv4 address from an integer to an InetAddress. * @param hostAddress an int corresponding to the IPv4 address in network byte order */ public static InetAddress intToInetAddress(int hostAddress) { byte[] addressBytes = { (byte)(0xff & hostAddress), (byte)(0xff & (hostAddress >> 8)), (byte)(0xff & (hostAddress >> 16)), (byte)(0xff & (hostAddress >> 24)) }; try { return InetAddress.getByAddress(addressBytes); } catch (UnknownHostException e) { throw new AssertionError(); } } 转载至: http://www.xuebuyuan.com/2046613.html
相关文章推荐
- 汇编语言集成开发环境略述
- Sublime2编辑器编写Python脚本的快捷操作
- 在PostgreSQL中如何生成线性相关的测试数据
- Hybrid app 浏览器跨域插件安装,以及网页调试技巧。
- 欢迎使用CSDN-markdown编辑器
- 自助机安卓主板系列-RK3288 Android4.4 如何获取 root权限
- 三大服务器对比
- 【转】海量用户积分排名算法探讨
- linux查看是什么操作系统是什么命令
- centos6通过yum安装nginx
- 登录模板
- RxJava 学习笔记
- 关于JS Lodop打印插件打印Bootstrap样式错乱问题的解决方案
- leetcode-2-AddTwoNumbers
- fatal error: sys/cdefs.h解决方案
- ios afnetworking https遇到的一些问题
- 利用反射获得类的public static/const成员的值
- 自定义UIAlertView
- 有关docker的学习链接
- cryptkeeper的使用