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?
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With