微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android : 有线DHCP信息保存到属性值

修改源码路径:frameworks\base\services\net\java\android\net\dhcp\DhcpClient.java

1、定义对应属性变量:

    // Patch Begin - Sheldon
    private static final String PROPERTY_DHCPCLIENT_IP      = "dhcp.eth0.ipaddress";
    private static final String PROPERTY_DHCPCLIENT_MASK    = "dhcp.eth0.mask";
    private static final String PROPERTY_DHCPCLIENT_GATEWAY = "dhcp.eth0.gateway";
    private static final String PROPERTY_DHCPCLIENT_DNS1    = "dhcp.eth0.dns1";
    private static final String PROPERTY_DHCPCLIENT_DNS2    = "dhcp.eth0.dns2";
    // Patch end

2.从acceptDhcpResults方法的results参数中提取IP地址、子网掩码、网关、DNS等信息。注:子网掩码是通过PrefixLength转换所得。

    private void acceptDhcpResults(DhcpResults results, String msg) {
        mDhcpLease = results;
        mOffer = null;
        Log.d(TAG, msg + " lease: " + mDhcpLease);

        // Patch Begin - Sheldon
        if (mIfaceName != null && (mIfaceName.equals("eth0") || mIfaceName.equals("veth0")))
        {
            try {
                    String ipAddr  = results.ipAddress.getAddress().getHostAddress();
                    int preFixLen  = results.ipAddress.getPrefixLength();

                    // Convert PrefixLength to subnet mask
                    int value = 0xffffffff << (32 - preFixLen);
                    byte[] bytes = new byte[]{ 
                            (byte)(value >> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff)};
                    InetAddress netAddr = InetAddress.getByAddress(bytes);
                    String subnetMask  = netAddr.getHostAddress();

                    String gateWay = deleteCharString(results.gateway.toString(), '/');
                    String dns1    = deleteCharString(results.dnsServers.get(0).toString(), '/');
                    String dns2    = deleteCharString(results.dnsServers.get(1).toString(), '/');

                    SystemProperties.set(PROPERTY_DHCPCLIENT_IP, ipAddr);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_MASK, subnetMask);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_GATEWAY, gateWay);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_DNS1, dns1);
                    SystemProperties.set(PROPERTY_DHCPCLIENT_DNS2, dns2);
           
            } catch (Exception e) {
                    e.printstacktrace();
            }
        }
        //Patch end

        notifySuccess();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐