Java Code Examples for android.net.ConnectivityManager#getNetworkCapabilities()
The following examples show how to use
android.net.ConnectivityManager#getNetworkCapabilities() .
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 Telegram-FOSS with GNU General Public License v2.0 | 6 votes |
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 2
Source File: AndroidNetworkLibrary.java From 365browser with Apache License 2.0 | 6 votes |
/** * 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 3
Source File: CheckNet.java From Ruisi with Apache License 2.0 | 6 votes |
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 cronet with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * 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 = ApiHelperForM.getActiveNetwork(connectivityManager); if (network == null) return false; NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); }
Example 5
Source File: WebviewInits.java From WebviewProject with MIT License | 6 votes |
static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (connectivityManager!=null) { NetworkCapabilities networkCapabilities=connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); if (networkCapabilities!=null) { if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) return true; else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) return true; else return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET); } else return false; }else return false; }else { NetworkInfo activeNetworkInfo = null; if (connectivityManager != null) activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } }
Example 6
Source File: Requirements.java From Telegram with GNU General Public License v2.0 | 6 votes |
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 7
Source File: Requirements.java From MediaSDK with Apache License 2.0 | 6 votes |
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 8
Source File: NetworkUtil.java From NClientV2 with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static ConnectionType getConnectivityPostLollipop(ConnectivityManager cm,Network network){ NetworkCapabilities capabilities=cm.getNetworkCapabilities(network); if(capabilities==null){return ConnectionType.WIFI;} if(capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) return ConnectionType.CELLULAR; return ConnectionType.WIFI; }
Example 9
Source File: WifiUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 获取连接的 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 10
Source File: NetWorkUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 获取连接的网络类型 * @return -1 = 等于未知, 1 = Wifi, 2 = 移动网络 */ @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public static int getConnectType() { try { // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 ) ConnectivityManager cManager = AppUtils.getConnectivityManager(); // 版本兼容处理 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { // 判断连接的是否 Wifi State wifiState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 判断是否连接上 if (wifiState == State.CONNECTED || wifiState == State.CONNECTING) { return 1; } else { // 判断连接的是否移动网络 State mobileState = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 判断移动网络是否连接上 if (mobileState == State.CONNECTED || mobileState == State.CONNECTING) { return 2; } } } else { // 获取当前活跃的网络 ( 连接的网络信息 ) Network network = cManager.getActiveNetwork(); if (network != null) { NetworkCapabilities networkCapabilities = cManager.getNetworkCapabilities(network); // 判断连接的是否 Wifi if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { return 1; } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { // 判断连接的是否移动网络 return 2; } } } } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getConnectType"); } return -1; }
Example 11
Source File: NetWorkReceiver.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 获取连接的网络类型 * @return 连接的网络类型 */ @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public static int getConnectType() { // 获取手机所有连接管理对象 ( 包括对 wi-fi,net 等连接的管理 ) try { // 获取网络连接状态 ConnectivityManager cManager = AppUtils.getConnectivityManager(); // 版本兼容处理 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { // 判断连接的是否 Wifi NetworkInfo.State wifiState = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 判断是否连接上 if (wifiState == NetworkInfo.State.CONNECTED || wifiState == NetworkInfo.State.CONNECTING) { return NET_WIFI; } else { // 判断连接的是否移动网络 NetworkInfo.State mobileState = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 判断移动网络是否连接上 if (mobileState == NetworkInfo.State.CONNECTED || mobileState == NetworkInfo.State.CONNECTING) { return NET_MOBILE; } } } else { // 获取当前活跃的网络 ( 连接的网络信息 ) Network network = cManager.getActiveNetwork(); if (network != null) { NetworkCapabilities networkCapabilities = cManager.getNetworkCapabilities(network); // 判断连接的是否 Wifi if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { return NET_WIFI; } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { return NET_MOBILE; } } } } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getConnectType"); } return NO_NETWORK; }
Example 12
Source File: MainActivity.java From Intra with Apache License 2.0 | 5 votes |
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 13
Source File: NetworkHelper.java From Hentoid with Apache License 2.0 | 5 votes |
@SuppressWarnings({"squid:CallToDeprecatedMethod"}) public static @Connectivity int getConnectivity(@NonNull Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (null == connectivityManager) return Connectivity.NO_INTERNET; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Network activeNetwork = connectivityManager.getActiveNetwork(); if (null == activeNetwork) return Connectivity.NO_INTERNET; NetworkCapabilities actNw = connectivityManager.getNetworkCapabilities(activeNetwork); if (null == actNw) return Connectivity.NO_INTERNET; // Below code _does not_ detect wifi properly when there's a VPN on -> using WifiManager instead (!) /* if (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) return Connectivity.WIFI; else return Connectivity.OTHER; */ WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager != null && wifiManager.isWifiEnabled() && wifiManager.getConnectionInfo().getBSSID() != null) return Connectivity.WIFI; else return Connectivity.OTHER; } else { NetworkInfo info = connectivityManager.getActiveNetworkInfo(); if (null == info) return Connectivity.NO_INTERNET; NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWifi != null && mWifi.isConnected()) return Connectivity.WIFI; else return Connectivity.OTHER; } }
Example 14
Source File: LocalIPUtil.java From DanDanPlayForAndroid with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private LinkProperties getLinkProperties(ConnectivityManager connectivityManager, int cap) { Network nets[] = connectivityManager.getAllNetworks(); for (Network n: nets) { LinkProperties linkProperties = connectivityManager.getLinkProperties(n); NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(n); String interfaceName = linkProperties.getInterfaceName(); if (interfaceName != null && networkCapabilities != null) { if (networkCapabilities.hasTransport(cap)) return linkProperties; } } return null; }
Example 15
Source File: ConnectivityHelper.java From libcommon with Apache License 2.0 | 5 votes |
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void updateActiveNetwork(@Nullable final Network network) { if (DEBUG) Log.v(TAG, "updateActiveNetwork:" + network); if (network != null) { final ConnectivityManager manager = requireConnectivityManager(); @Nullable final NetworkCapabilities capabilities = manager.getNetworkCapabilities(network); // API>=21 // FIXME API>=29でNetworkInfoがdeprecatedなので対策を追加する @Nullable final NetworkInfo info = manager.getNetworkInfo(network); // API>=21 int activeNetworkType = NETWORK_TYPE_NON; if ((capabilities != null) && (info != null)) { if (isWifiNetworkReachable(capabilities, info)) { activeNetworkType = NETWORK_TYPE_WIFI; } else if (isMobileNetworkReachable(capabilities, info)) { activeNetworkType = NETWORK_TYPE_MOBILE; } else if (isBluetoothNetworkReachable(capabilities, info)) { activeNetworkType = NETWORK_TYPE_BLUETOOTH; } else if (isNetworkReachable(capabilities, info)) { activeNetworkType = NETWORK_TYPE_ETHERNET; } } updateActiveNetwork(activeNetworkType); } else { updateActiveNetwork(NETWORK_TYPE_NON); } }
Example 16
Source File: ThetaM15.java From DeviceConnect-Android with MIT License | 5 votes |
private static SocketFactory getWifiSocketFactory(final Context context) { SocketFactory socketFactory = SocketFactory.getDefault(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isGalaxyDevice()) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] allNetwork = cm.getAllNetworks(); for (Network network : allNetwork) { NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network); if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { socketFactory = network.getSocketFactory(); } } } return socketFactory; }
Example 17
Source File: NetworkUtils.java From sa-sdk-android with Apache License 2.0 | 5 votes |
/** * 是否有可用网络 * * @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 18
Source File: Device.java From android-job with Apache License 2.0 | 5 votes |
@SuppressWarnings("deprecation") private static boolean isRoaming(ConnectivityManager connectivityManager, NetworkInfo networkInfo) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { return networkInfo.isRoaming(); } try { NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); } catch (Exception e) { return networkInfo.isRoaming(); } }
Example 19
Source File: WiFiSocketFactory.java From sdl_java_suite with BSD 3-Clause "New" or "Revised" License | 4 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Socket createWiFiSocket(Context context) { if(context == null){ logInfo("Context supplied was null"); return null; } PackageManager pm = context.getPackageManager(); if (pm == null) { logInfo("PackageManager isn't available."); return null; } // getAllNetworks() and getNetworkCapabilities() require ACCESS_NETWORK_STATE if (pm.checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, context.getPackageName()) != PackageManager.PERMISSION_GRANTED) { logInfo("Router service doesn't have ACCESS_NETWORK_STATE permission. It cannot bind a TCP transport to Wi-Fi network."); return null; } ConnectivityManager connMan = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connMan == null) { logInfo("ConnectivityManager isn't available."); return null; } Network[] allNetworks = connMan.getAllNetworks(); if (allNetworks == null) { logInfo("Failed to acquire a list of networks."); return null; } // Samsung Galaxy S9 (with Android 8.0.0) provides two `Network` instances which have // TRANSPORT_WIFI capability. The first one throws an IOException upon creating a Socket, // and the second one actually works. To support such case, here we iterate over all // `Network` instances until we can create a Socket. for (Network network : allNetworks) { if (network == null) { continue; } NetworkCapabilities capabilities = connMan.getNetworkCapabilities(network); if (capabilities == null) { continue; } if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { try { SocketFactory factory = network.getSocketFactory(); if (factory != null) { return factory.createSocket(); } } catch (IOException e) { logInfo("IOException during socket creation (ignored): " + e.getMessage()); } } } logInfo("Cannot find Wi-Fi network to bind a TCP transport."); return null; }
Example 20
Source File: Vpn.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
@VisibleForTesting public static void applyUnderlyingCapabilities( ConnectivityManager cm, Network[] underlyingNetworks, NetworkCapabilities caps) { int[] transportTypes = new int[] { NetworkCapabilities.TRANSPORT_VPN }; int downKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; int upKbps = NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED; boolean metered = false; boolean roaming = false; boolean congested = false; boolean hadUnderlyingNetworks = false; if (null != underlyingNetworks) { for (Network underlying : underlyingNetworks) { // TODO(b/124469351): Get capabilities directly from ConnectivityService instead. final NetworkCapabilities underlyingCaps = cm.getNetworkCapabilities(underlying); if (underlyingCaps == null) continue; hadUnderlyingNetworks = true; for (int underlyingType : underlyingCaps.getTransportTypes()) { transportTypes = ArrayUtils.appendInt(transportTypes, underlyingType); } // When we have multiple networks, we have to assume the // worst-case link speed and restrictions. downKbps = NetworkCapabilities.minBandwidth(downKbps, underlyingCaps.getLinkDownstreamBandwidthKbps()); upKbps = NetworkCapabilities.minBandwidth(upKbps, underlyingCaps.getLinkUpstreamBandwidthKbps()); metered |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_METERED); roaming |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_ROAMING); congested |= !underlyingCaps.hasCapability(NET_CAPABILITY_NOT_CONGESTED); } } if (!hadUnderlyingNetworks) { // No idea what the underlying networks are; assume sane defaults metered = true; roaming = false; congested = false; } caps.setTransportTypes(transportTypes); caps.setLinkDownstreamBandwidthKbps(downKbps); caps.setLinkUpstreamBandwidthKbps(upKbps); caps.setCapability(NET_CAPABILITY_NOT_METERED, !metered); caps.setCapability(NET_CAPABILITY_NOT_ROAMING, !roaming); caps.setCapability(NET_CAPABILITY_NOT_CONGESTED, !congested); }