Example usage for android.net LinkProperties getDnsServers

List of usage examples for android.net LinkProperties getDnsServers

Introduction

In this page you can find the example usage for android.net LinkProperties getDnsServers.

Prototype

public @NonNull List<InetAddress> getDnsServers() 

Source Link

Document

Returns all the InetAddress for DNS servers on this link.

Usage

From source file:eu.faircode.netguard.Util.java

public static List<String> getDefaultDNS(Context context) {
    String dns1 = null;//from   w  ww  .ja v  a 2  s. c om
    String dns2 = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network an = cm.getActiveNetwork();
        if (an != null) {
            LinkProperties lp = cm.getLinkProperties(an);
            if (lp != null) {
                List<InetAddress> dns = lp.getDnsServers();
                if (dns != null) {
                    if (dns.size() > 0)
                        dns1 = dns.get(0).getHostAddress();
                    if (dns.size() > 1)
                        dns2 = dns.get(1).getHostAddress();
                    for (InetAddress d : dns)
                        Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                }
            }
        }
    } else {
        dns1 = jni_getprop("net.dns1");
        dns2 = jni_getprop("net.dns2");
    }

    List<String> listDns = new ArrayList<>();
    listDns.add(TextUtils.isEmpty(dns1) ? "8.8.8.8" : dns1);
    listDns.add(TextUtils.isEmpty(dns2) ? "8.8.4.4" : dns2);
    return listDns;
}