Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the current DNS servers for Android?

Tags:

android

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 this but how can I get hold of a valid DhcpInfo object?

like image 502
John Avatar asked Jun 18 '10 13:06

John


People also ask

How do I find my current DNS server?

Open your Command Prompt from the Start menu (or type “Cmd” into the search in your Windows task bar). Next, type ipconfig/all into your command prompt and press Enter. Look for the field labeled “DNS Servers.” The first address is the primary DNS server, and the next address is the secondary DNS server.

How do I find my secondary DNS on Android?

To see or edit the DNS settings on your Android phone or tablet, tap the "Settings" menu on your home screen. Tap "Wi-Fi" to access your network settings, then press and hold the network you want to configure and tap "Modify Network." Tap "Show Advanced Settings" if this option appears.


1 Answers

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); } 
like image 65
A-IV Avatar answered Sep 28 '22 08:09

A-IV