How do you get the current DNS servers for Android?

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<String> servers = new ArrayList<String>();
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);
}

Leave a Comment