Example usage for android.net ConnectivityManager getActiveNetwork

List of usage examples for android.net ConnectivityManager getActiveNetwork

Introduction

In this page you can find the example usage for android.net ConnectivityManager getActiveNetwork.

Prototype

@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public Network getActiveNetwork() 

Source Link

Document

Returns a Network object corresponding to the currently active default data network.

Usage

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

public static List<String> getDefaultDNS(Context context) {
    String dns1 = null;//from   www  .j ava 2  s .  co  m
    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;
}