android获取wifi的IP,子网掩码,网关,dns等信息

使用 DhcpInfo 获取这些信息,返回的是一个int型数据,转换下得到想要的信息,代码如下:

[java]  view plain copy print ?
  1. public class MainActivity extends Activity {  
  2.       
  3.     private WifiManager my_wifiManager;  
  4.     private WifiInfo wifiInfo;  
  5.     private DhcpInfo dhcpInfo;  
  6.   
  7.     private TextView tvResult;  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.   
  14.         my_wifiManager = ((WifiManager) getSystemService("wifi"));  
  15.         dhcpInfo = my_wifiManager.getDhcpInfo();  
  16.         wifiInfo = my_wifiManager.getConnectionInfo();  
  17.   
  18.         tvResult = (TextView) findViewById(R.id.tvResult);  
  19.     }  
  20.   
  21.     @Override  
  22.     protected void onResume() {  
  23.         super.onResume();  
  24.         StringBuilder sb = new StringBuilder();  
  25.         sb.append("网络信息:");  
  26.         sb.append("\nipAddress:" + intToIp(dhcpInfo.ipAddress));  
  27.         sb.append("\nnetmask:" + intToIp(dhcpInfo.netmask));  
  28.         sb.append("\ngateway:" + intToIp(dhcpInfo.gateway));  
  29.         sb.append("\nserverAddress:" + intToIp(dhcpInfo.serverAddress));  
  30.         sb.append("\ndns1:" + intToIp(dhcpInfo.dns1));  
  31.         sb.append("\ndns2:" + intToIp(dhcpInfo.dns2));  
  32.         sb.append("\n");  
  33.         System.out.println(intToIp(dhcpInfo.ipAddress));  
  34.         System.out.println(intToIp(dhcpInfo.netmask));  
  35.         System.out.println(intToIp(dhcpInfo.gateway));  
  36.         System.out.println(intToIp(dhcpInfo.serverAddress));  
  37.         System.out.println(intToIp(dhcpInfo.dns1));  
  38.         System.out.println(intToIp(dhcpInfo.dns2));  
  39.         System.out.println(dhcpInfo.leaseDuration);  
  40.   
  41.         sb.append("Wifi信息:");  
  42.         sb.append("\nIpAddress:" + intToIp(wifiInfo.getIpAddress()));  
  43.         sb.append("\nMacAddress:" + wifiInfo.getMacAddress());  
  44.         tvResult.setText(sb.toString());  
  45.     }  
  46.   
  47.     private String intToIp(int paramInt) {  
  48.         return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."  
  49.                 + (0xFF & paramInt >> 24);  
  50.     }  
  51. }  

效果如下:


备注:

android.net.NetworkUtils

[java]  view plain copy print ?
  1. /** 
  2.  * Convert a IPv4 address from an integer to an InetAddress. 
  3.  * @param hostAddress an int corresponding to the IPv4 address in network byte order 
  4.  */  
  5. public static InetAddress intToInetAddress(int hostAddress) {  
  6.     byte[] addressBytes = { (byte)(0xff & hostAddress),  
  7.                             (byte)(0xff & (hostAddress >> 8)),  
  8.                             (byte)(0xff & (hostAddress >> 16)),  
  9.                             (byte)(0xff & (hostAddress >> 24)) };  
  10.   
  11.     try {  
  12.        return InetAddress.getByAddress(addressBytes);  
  13.     } catch (UnknownHostException e) {  
  14.        throw new AssertionError();  
  15.     }  
  16. }  


附件下载:

http://download.csdn.net/detail/ttdevs/5805127

转载地址:http://blog.csdn.net/ttdevs/article/details/9445695

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值