List of usage examples for android.net ConnectivityManager getLinkProperties
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public LinkProperties getLinkProperties(@Nullable Network network)
From source file:eu.faircode.netguard.Util.java
public static List<String> getDefaultDNS(Context context) { String dns1 = null;/* ww w. j a va2 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; }