您现在的位置: 爱51代码网 >> 范文 >> 文章正文
如何读取android设备的 子网掩码和默认网关

如何读取android设备的 子网掩码和默认网关

//获取mac地址 public String getLocalMacAddress()      {                               WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);           WifiInfo info = wifi.getConnectionInfo();           return info.getMacAddress();       }      //获取ip地址     public String getLocalIpAddress()      {           try {               for (Enumeration<NetworkInterface> en = NetworkInterface                       .getNetworkInterfaces(); en.hasMoreElements();) {                   NetworkInterface intf = en.nextElement();                   for (Enumeration<InetAddress> enumIpAddr = intf                           .getInetAddresses(); enumIpAddr.hasMoreElements();) {                       InetAddress inetAddress = enumIpAddr.nextElement();                       if (!inetAddress.isLoopbackAddress()) {                           return inetAddress.getHostAddress().toString();                       }                   }               }           } catch (SocketException ex)          {               Log.e("WifiPreference IpAddress", ex.toString());           }           return null;       }  


如何获取子网掩码和默认网关呢

android.net package中的DdhcpInfo类,存储当前网络状态的状态参数信息,但是返回值是integer 类型,需要自己转换一下

package com.schogini.dhcp;   import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.TextView; import android.net.*; import android.net.wifi.WifiManager;   public class dhcpInfo extends Activity {     public String   s_dns1 ;     public String   s_dns2;          public String   s_gateway;       public String   s_ipAddress;         public String   s_leaseDuration;         public String   s_netmask;       public String   s_serverAddress;     TextView info;     DhcpInfo d;     WifiManager wifii;       /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);         d=wifii.getDhcpInfo();           s_dns1="DNS 1: "+String.valueOf(d.dns1);         s_dns2="DNS 2: "+String.valueOf(d.dns2);             s_gateway="Default Gateway: "+String.valueOf(d.gateway);             s_ipAddress="IP Address: "+String.valueOf(d.ipAddress);          s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);              s_netmask="Subnet Mask: "+String.valueOf(d.netmask);             s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);           //dispaly them         info= (TextView) findViewById(R.id.infolbl);         info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);     } }

 

XML/HTML code?<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="      package="com.schogini.dhcp"      android:versionCode="1"      android:versionName="1.0">     <uses-sdk android:minSdkVersion="4" />       <application android:icon="@drawable/icon" android:label="@string/app_name">         <activity android:name=".dhcpInfo"                  android:label="@string/app_name">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />   </manifest>

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    undefined reference timer_crea
    linux文件/usr/lib破坏了,还原后
    linux上运行system函数时,print
    Failed to open eth0
    android手机无法与eclipse或电脑
    C/C++洗牌算法源代码
    servlet技术实现用户名唯一的验证
    E-business suite system servic
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    sharepoint 2010 获取用户信息Us
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    Android adb shell中将getev
    assignment makes integer f
    Qt线程里面如何给一个结构体
    qt的connect(timer,SIGNAL(t
    无法访问android.app.Activi
    android子线程中处理GPS代码
    QT 获取鼠标点击对象信息问题
    如何让一个service监听按键事
    android程序运行崩溃,然后不
    监听去电状态的时候状态直接
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |