[ACCEPTED]-How do I find the DNS servers in Android from a Java program?-java

Accepted answer
Score: 15

The DNS protocol is not that complex - can't you just 21 do the DNS accesses using raw sockets (either 20 TCP or UDP)? After a quick look at the 19 dnsjava doco it seems to provide low level 18 DNS support to assist with this.

The other 17 possible direction is, starting with dnsjava, to 16 remove the dependence on /etc/resolv.conf. I would think 15 about using getprop in your launch script 14 to set properties in the JVM, or to create 13 a localized resolv.conf file in your app's directory 12 from which you can read the information 11 needed. In other words, use getprop to 10 inject information into the JVM instead 9 of attempting to pull it in once the JVM 8 is going. Surely creating a file that dnsjava 7 can use directly should be doable.


Edit - android.net

It looks 6 like android.net.ConnectivityManager will deliver you an array of NetworkInfo's 5 using getAllNetworkInfo(). Then use android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given 4 network interface - the DhcpInfo structure has the 3 IP address for dns1 and dns2 for that interface. Surprised 2 that the DNS's are int, therefore implying 1 IP4 only, though.

Score: 5

I don't think it's possible for general 1 case. For WiFi I found this:

WiFiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE); 
DhcpInfo info = wifi.getDhcpInfo(); 
Score: 2

To get system properties of an android system, static 7 methods in this class (android.os.SystemProperties) can be used. As 6 it is not an open api, you can't compile 5 it against the sdk. Work around is needed 4 to do that, e.g. copy the code from AOSP 3 to get thru the compilation. As this class 2 is for internal use, do this at your own 1 risk. Have fun.

Score: 1

We should be able to find dns address being 7 irrespective to what network (wifi or ethernet) we 6 are connected. Here is my program.

In your 5 AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Your code:

    ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Service.CONNECTIVITY_SERVICE);

                /* you can print your active network via using below */
                Log.i("myNetworkType: ", connectivityManager.getActiveNetworkInfo().getTypeName());
                WifiManager wifiManager= (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);


                Log.i("routes ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getRoutes().toString());
                Log.i("domains ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDomains().toString());
                Log.i("ip address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses().toString());
                Log.i("dns address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDnsServers().toString());



                if(connectivityManager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
                    Log.i("myType ", "wifi");
                    DhcpInfo d =wifiManager.getDhcpInfo();
                    Log.i("info", d.toString()+"");
                }
                else if(connectivityManager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET) {
/* there is no EthernetManager class, there is only WifiManager. so, I used this below trick to get my IP range, dns, gateway address etc */

                    Log.i("myType ", "Ethernet");
                    Log.i("routes ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getRoutes().toString());
                    Log.i("domains ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDomains().toString());
                    Log.i("ip address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses().toString());
                    Log.i("dns address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDnsServers().toString());

                }
                else {

                }

Output enter image description here

You can't reach to know whether you 4 are connected via wifi or network using 3 WifiManager as WifiManager only deals with wifi. You have to use 2 ConnectivityManager. I updated the code again where I merged 1 WifiManager and ConnectivityManager to produce the result that you wanted.

More Related questions