Java Code Examples for android.net.ConnectivityManager#getActiveNetwork()

The following examples show how to use android.net.ConnectivityManager#getActiveNetwork() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 7 votes vote down vote up
private static boolean checkInternetConnectivity(ConnectivityManager connectivityManager) {
  if (Util.SDK_INT < 23) {
    // TODO Check internet connectivity using http://clients3.google.com/generate_204 on API
    // levels prior to 23.
    return true;
  }
  Network activeNetwork = connectivityManager.getActiveNetwork();
  if (activeNetwork == null) {
    logd("No active network.");
    return false;
  }
  NetworkCapabilities networkCapabilities =
      connectivityManager.getNetworkCapabilities(activeNetwork);
  boolean validated =
      networkCapabilities == null
          || !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
  logd("Network capability validated: " + validated);
  return !validated;
}
 
Example 2
Source File: Requirements.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isInternetConnectivityValidated(ConnectivityManager connectivityManager) {
  if (Util.SDK_INT < 23) {
    // TODO Check internet connectivity using http://clients3.google.com/generate_204 on API
    // levels prior to 23.
    return true;
  }
  Network activeNetwork = connectivityManager.getActiveNetwork();
  if (activeNetwork == null) {
    return false;
  }
  NetworkCapabilities networkCapabilities =
      connectivityManager.getNetworkCapabilities(activeNetwork);
  boolean validated =
      networkCapabilities == null
          || !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
  return !validated;
}
 
Example 3
Source File: CheckNet.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
private void request(final CheckNetResponse checkNetResponse) {
    finishCount = 0;
    errCount = 0;


    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        Network network = connectivityManager.getActiveNetwork();
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
        if (capabilities == null) {
            checkNetResponse.sendFinishMessage(0, "无法连接到睿思,请打开网络连接");
        } //else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
        checkSchoolNet(context);
        checkOutNet(context);
    } else {
        final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            checkSchoolNet(context);
            checkOutNet(context);
        } else {
            checkNetResponse.sendFinishMessage(0, "无法连接到睿思,请打开网络连接");
        }
    }
}
 
Example 4
Source File: AndroidNetworkLibrary.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the system's captive portal probe was blocked for the current default data
 * network. The method will return false if the captive portal probe was not blocked, the login
 * process to the captive portal has been successfully completed, or if the captive portal
 * status can't be determined. Requires ACCESS_NETWORK_STATE permission. Only available on
 * Android Marshmallow and later versions. Returns false on earlier versions.
 */
@TargetApi(Build.VERSION_CODES.M)
@CalledByNative
private static boolean getIsCaptivePortal() {
    // NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL is only available on Marshmallow and
    // later versions.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;
    ConnectivityManager connectivityManager =
            (ConnectivityManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) return false;

    Network network = connectivityManager.getActiveNetwork();
    if (network == null) return false;

    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
    return capabilities != null
            && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
}
 
Example 5
Source File: Requirements.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isInternetConnectivityValidated(ConnectivityManager connectivityManager) {
  if (Util.SDK_INT < 23) {
    // TODO Check internet connectivity using http://clients3.google.com/generate_204 on API
    // levels prior to 23.
    return true;
  }
  Network activeNetwork = connectivityManager.getActiveNetwork();
  if (activeNetwork == null) {
    return false;
  }
  NetworkCapabilities networkCapabilities =
      connectivityManager.getNetworkCapabilities(activeNetwork);
  boolean validated =
      networkCapabilities == null
          || !networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
  return !validated;
}
 
Example 6
Source File: AndroidNetworkLibrary.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
@CalledByNative
private static byte[][] getDnsServers() {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return new byte[0][0];
    }
    Network network = connectivityManager.getActiveNetwork();
    if (network == null) {
        return new byte[0][0];
    }
    LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
    if (linkProperties == null) {
        return new byte[0][0];
    }
    List<InetAddress> dnsServersList = linkProperties.getDnsServers();
    byte[][] dnsServers = new byte[dnsServersList.size()][];
    for (int i = 0; i < dnsServersList.size(); i++) {
        dnsServers[i] = dnsServersList.get(i).getAddress();
    }
    return dnsServers;
}
 
Example 7
Source File: MainActivity.java    From Intra with Apache License 2.0 6 votes vote down vote up
private LinkProperties getLinkProperties() {
  ConnectivityManager connectivityManager =
      (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    // getActiveNetwork() requires M or later.
    return null;
  }
  Network activeNetwork = connectivityManager.getActiveNetwork();
  if (activeNetwork == null) {
    return null;
  }
  return connectivityManager.getLinkProperties(activeNetwork);
}
 
Example 8
Source File: NetworkUtils.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 是否有可用网络
 *
 * @param context Context
 * @return true:网络可用,false:网络不可用
 */
public static boolean isNetworkAvailable(Context context) {
    // 检测权限
    if (!SensorsDataUtils.checkHasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
        return false;
    }
    try {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Network network = cm.getActiveNetwork();
                if (network != null) {
                    NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
                    if (capabilities != null) {
                        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
                                || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
                    }
                }
            } else {
                NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                return networkInfo != null && networkInfo.isConnected();
            }
        }
        return false;
    } catch (Exception e) {
        SALog.printStackTrace(e);
        return false;
    }
}
 
Example 9
Source File: JNIUtilities.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(23)
public static String getCurrentNetworkInterfaceName(){
	ConnectivityManager cm=(ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	Network net=cm.getActiveNetwork();
	if(net==null)
		return null;
	LinkProperties props=cm.getLinkProperties(net);
	if(props==null)
		return null;
	return props.getInterfaceName();
}
 
Example 10
Source File: JNIUtilities.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(23)
public static String getCurrentNetworkInterfaceName(){
	ConnectivityManager cm=(ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	Network net=cm.getActiveNetwork();
	if(net==null)
		return null;
	LinkProperties props=cm.getLinkProperties(net);
	if(props==null)
		return null;
	return props.getInterfaceName();
}
 
Example 11
Source File: MainActivity.java    From Intra with Apache License 2.0 5 votes vote down vote up
private boolean isAnotherVpnActive() {
  if (VERSION.SDK_INT >= VERSION_CODES.M) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    Network activeNetwork = connectivityManager.getActiveNetwork();
    if (activeNetwork == null) {
      return false;
    }
    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
    if (capabilities == null) {
      // It's not clear when this can happen, but it has occurred for at least one user.
      return false;
    }
    return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
  }
  // For pre-M versions, return true if there's any network whose name looks like a VPN.
  try {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
      NetworkInterface networkInterface = networkInterfaces.nextElement();
      String name = networkInterface.getName();
      if (networkInterface.isUp() && name != null &&
          (name.startsWith("tun") || name.startsWith("pptp") || name.startsWith("l2tp"))) {
        return true;
      }
    }
  } catch (SocketException e) {
    LogWrapper.logException(e);
  }
  return false;
}
 
Example 12
Source File: JNIUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(23)
public static String getCurrentNetworkInterfaceName(){
	ConnectivityManager cm=(ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	Network net=cm.getActiveNetwork();
	if(net==null)
		return null;
	LinkProperties props=cm.getLinkProperties(net);
	if(props==null)
		return null;
	return props.getInterfaceName();
}
 
Example 13
Source File: Util.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public static List<String> getDefaultDNS(Context context) {
    List<String> listDns = new ArrayList<>();

    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)
                    for (InetAddress d : dns) {
                        Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                        listDns.add(d.getHostAddress().split("%")[0]);
                    }
            }
        }
    } else {
        String dns1 = jni_getprop("net.dns1");
        String dns2 = jni_getprop("net.dns2");
        if (dns1 != null)
            listDns.add(dns1.split("%")[0]);
        if (dns2 != null)
            listDns.add(dns2.split("%")[0]);
    }

    return listDns;
}
 
Example 14
Source File: NetWorkUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否连接了网络
 * @return {@code true} yes, {@code false} no
 */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public static boolean isConnect() {
    try {
        // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 )
        ConnectivityManager cManager = AppUtils.getConnectivityManager();
        // 版本兼容处理
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            // 获取网络连接管理的对象
            NetworkInfo nInfo = cManager.getActiveNetworkInfo();
            // 判断是否为 null
            if (nInfo != null) {
                // 判断当前网络是否已经连接
                if (nInfo.getState() == State.CONNECTED) {
                    return true;
                }
            }
        } else {
            // 获取当前活跃的网络 ( 连接的网络信息 )
            Network network = cManager.getActiveNetwork();
            // 判断是否为 null
            if (network != null) {
                return true;
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "isConnect");
    }
    return false;
}
 
Example 15
Source File: WifiUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取连接的 Wifi 热点 SSID
 * @return Wifi 热点 SSID
 */
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public static String isConnectAphot() {
    try {
        // 连接管理
        ConnectivityManager cManager = AppUtils.getConnectivityManager();
        // 版本兼容处理
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            // 连接状态
            NetworkInfo.State nState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            if ((nState == NetworkInfo.State.CONNECTED)) {
                // 获取连接的 ssid
                return getSSID();
            }
        } else {
            // 获取当前活跃的网络 ( 连接的网络信息 )
            Network network = cManager.getActiveNetwork();
            if (network != null) {
                NetworkCapabilities networkCapabilities = cManager.getNetworkCapabilities(network);
                // 判断是否连接 Wifi
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    // 获取连接的 ssid
                    return getSSID();
                }
            }
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "isConnectAphot");
    }
    return null;
}
 
Example 16
Source File: DnsHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static String getDnsServer(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return DEFAULT_DNS;

    LinkProperties props = null;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        for (Network network : cm.getAllNetworks()) {
            NetworkInfo ni = cm.getNetworkInfo(network);
            if (ni != null && ni.isConnected()) {
                props = cm.getLinkProperties(network);
                Log.i("Old props=" + props);
                break;
            }
        }
    else {
        Network active = cm.getActiveNetwork();
        if (active == null)
            return DEFAULT_DNS;
        props = cm.getLinkProperties(active);
        Log.i("New props=" + props);
    }

    if (props == null)
        return DEFAULT_DNS;

    List<InetAddress> dns = props.getDnsServers();
    if (dns.size() == 0)
        return DEFAULT_DNS;
    else
        return dns.get(0).getHostAddress();
}
 
Example 17
Source File: Util.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
public static List<String> getDefaultDNS(Context context) {
    List<String> listDns = new ArrayList<>();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network an = null;
        if (cm != null) {
            an = cm.getActiveNetwork();
        }
        if (an != null) {
            LinkProperties lp = cm.getLinkProperties(an);
            if (lp != null) {
                List<InetAddress> dns = lp.getDnsServers();
                for (InetAddress d : dns) {
                    Log.i(LOG_TAG, "DNS from LP: " + d.getHostAddress());
                    listDns.add(d.getHostAddress().split("%")[0]);
                }
            }
        }
    } else {
        String dns1 = jni_getprop("net.dns1");
        String dns2 = jni_getprop("net.dns2");
        if (dns1 != null)
            listDns.add(dns1.split("%")[0]);
        if (dns2 != null)
            listDns.add(dns2.split("%")[0]);
    }

    return listDns;
}
 
Example 18
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public static List<String> getDefaultDNS(Context context) {
    List<String> listDns = new ArrayList<>();

    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)
                    for (InetAddress d : dns) {
                        Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                        listDns.add(d.getHostAddress().split("%")[0]);
                    }
            }
        }
    } else {
        String dns1 = jni_getprop("net.dns1");
        String dns2 = jni_getprop("net.dns2");
        if (dns1 != null)
            listDns.add(dns1.split("%")[0]);
        if (dns2 != null)
            listDns.add(dns2.split("%")[0]);
    }

    return listDns;
}
 
Example 19
Source File: AndroidUsingLinkProperties.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(23)
private static Network getActiveNetwork(ConnectivityManager cm) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? cm.getActiveNetwork() : null;
}
 
Example 20
Source File: AndroidUsingLinkProperties.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(23)
private static Network getActiveNetwork(ConnectivityManager cm) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? cm.getActiveNetwork() : null;
}