How do you get the current DNS servers for Android?

后端 未结 8 787
孤街浪徒
孤街浪徒 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条回答
  •  -上瘾入骨i
    2020-11-30 03:55

    Calling for the getRuntime().exec can hang your application.

    android.net.NetworkUtils.runDhcp() cause unnecessary network requests.

    So I prefer to do this:

    Class SystemProperties = Class.forName("android.os.SystemProperties");
    Method method = SystemProperties.getMethod("get", new Class[] { String.class });
    ArrayList servers = new ArrayList();
    for (String name : new String[] { "net.dns1", "net.dns2", "net.dns3", "net.dns4", }) {
        String value = (String) method.invoke(null, name);
        if (value != null && !"".equals(value) && !servers.contains(value))
            servers.add(value);
    }
    

提交回复
热议问题