通过WifiManager,可以获取DhcpInfo参数,

从而获取android IP地址及网关等信息 。

DhcpInfo参数中,gateway,netmask,ipAddress都是int型。

因此需要进行类型转换,转换成诸如172.22.198.5的IP地址。

Android已经提供了类型转换方法,即:

android.text.format.Formatter.formatIpAddress(dhcpInfo.ipAddress)等;

public String getIPAddressInfo() {WifiManager wifi_service = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);DhcpInfo dhcpInfo = wifi_service.getDhcpInfo();WifiInfo wifiinfo = wifi_service.getConnectionInfo();Log.d(TAG, "Wifi info----->" + wifiinfo.getIpAddress());Log.d(TAG,"DHCP info gateway----->"+ Formatter.formatIpAddress(dhcpInfo.gateway));Log.d(TAG,"DHCP info netmask----->"+ Formatter.formatIpAddress(dhcpInfo.netmask));Log.d(TAG,"DHCP info ipAddress----->"+ Formatter.formatIpAddress(dhcpInfo.ipAddress));return Formatter.formatIpAddress(dhcpInfo.ipAddress);}


但dhcpinfo参数值如何转换为相应的int型呢?

原理参见下图:

将Android DHCPInfo 属性转换为int类型(使用BigInteger)_第1张图片

(图片来源:http://www.bennadel.com/blog/1830-Converting-IP-Addresses-To-And-From-Integer-Values-With-ColdFusion.htm


原文中没有涉及java转换方法,自己补充一个方法:

public static long FormatDHCPIp(int fir,int sec,int thd,int four){BigInteger s = BigInteger.valueOf(fir);s = s.add(BigInteger.valueOf(sec*256)).add(BigInteger.valueOf(thd*256*256)).add(BigInteger.valueOf(four*256*256*256));System.out.println("s.longValue():"+s.longValue());    return s.longValue();}


制作成APK,网络参数和对应的int值一目了然:

将Android DHCPInfo 属性转换为int类型(使用BigInteger)_第2张图片


对应的apk下载地址:http://download.csdn.net/detail/singleton1900/6924931





更多相关文章

  1. 〖Android〗简单隐藏Android虚拟键盘的方法
  2. android 引入jni 的so库的方法
  3. Android屏幕保持常亮的三种方法
  4. Android 关闭/打开多点触控 (Android中设置不能同时点击2个view的
  5. Android命令行获取WiFi列表以及参数
  6. android 上调试动态库方法

随机推荐

  1. Android(安卓)Preference使用
  2. Android仿印象笔记的自定义菜单控件
  3. 近百个Android开发教程和资料收集整理打
  4. Java大文件上传(Android亦可)
  5. 横竖屏切换时候activity的生命周期?
  6. Android(安卓)Dialog的7种形式
  7. 最受欢迎的文章汇总
  8. Mac下Android(安卓)Studio使用
  9. 我想给你们介绍一个与众不同的Android微
  10. Android之Wifi学习(1)