How do I find the DNS servers in Android from a Java program?

前端 未结 4 1219
梦谈多话
梦谈多话 2021-02-19 00:41

The java.net.InetAddress.GetByName(String host) method can only return A records so to lookup other record types I need to be able to send DNS queries

4条回答
  •  广开言路
    2021-02-19 01:15

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

    In your AndroidManifest.xml file

    
    
    

    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

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

提交回复
热议问题