How do you get the current DNS servers for Android?

后端 未结 8 799
孤街浪徒
孤街浪徒 2020-11-30 03:01

I\'m trying to get hold of the addresses to the currently used DNS servers in my application, either I\'m connected thru Wifi or mobile. The DhcpInfo object should provide t

8条回答
  •  野性不改
    2020-11-30 03:48

    A native alternative is:

    char dns1[PROP_VALUE_MAX];
    __system_property_get("net.dns1", dns1);
    

    Or better yet for a comprehensive list:

    for (i = 1; i <= MAX_DNS_PROPERTIES; i++) {
        char prop_name[PROP_NAME_MAX];
        snprintf(prop_name, sizeof(prop_name), "net.dns%d", i);
        __system_property_get(prop_name, dns);
    }
    

    There are a few advantages to doing it this way:

    1. runDHCP is really slow. It can take as long as 5-10 seconds. This can cause a major hang when used incorrectly.
    2. runDCHP doesn't seem to work for 3G/4G.
    3. Since runDCHP is a hidden API it is subject to change. In fact it did change in ICS. In ICS it takes a new DhcpInfoInternal, so you'll have to create two different to support all phones.

提交回复
热议问题