本文整理汇总了Java中android.net.NetworkCapabilities.hasCapability方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkCapabilities.hasCapability方法的具体用法?Java NetworkCapabilities.hasCapability怎么用?Java NetworkCapabilities.hasCapability使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.NetworkCapabilities
的用法示例。
在下文中一共展示了NetworkCapabilities.hasCapability方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hasInternetConnection
import android.net.NetworkCapabilities; //导入方法依赖的package包/类
/**
* @param context the current application context
* @return whether the device was connected to the internet
*/
public static boolean hasInternetConnection(Context context) {
/*
Taken from Johan's answer at: https://stackoverflow.com/a/35009615
*/
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network network;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
network = connectivityManager.getActiveNetwork();
} else
return true;
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
示例2: getIsCaptivePortal
import android.net.NetworkCapabilities; //导入方法依赖的package包/类
/**
* 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);
}
示例3: getAllNetworksFiltered
import android.net.NetworkCapabilities; //导入方法依赖的package包/类
/**
* Returns all connected networks that are useful and accessible to Chrome.
* Only callable on Lollipop and newer releases.
* @param ignoreNetwork ignore this network as if it is not connected.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Network[] getAllNetworksFiltered(
ConnectivityManagerDelegate connectivityManagerDelegate, Network ignoreNetwork) {
Network[] networks = connectivityManagerDelegate.getAllNetworksUnfiltered();
// Whittle down |networks| into just the list of networks useful to us.
int filteredIndex = 0;
for (Network network : networks) {
if (network.equals(ignoreNetwork)) {
continue;
}
final NetworkCapabilities capabilities =
connectivityManagerDelegate.getNetworkCapabilities(network);
if (capabilities == null || !capabilities.hasCapability(NET_CAPABILITY_INTERNET)) {
continue;
}
if (capabilities.hasTransport(TRANSPORT_VPN)) {
// If we can access the VPN then...
if (connectivityManagerDelegate.vpnAccessible(network)) {
// ...we cannot access any other network, so return just the VPN.
return new Network[] {network};
} else {
// ...otherwise ignore it as we cannot use it.
continue;
}
}
networks[filteredIndex++] = network;
}
return Arrays.copyOf(networks, filteredIndex);
}
示例4: hasInternetCapability
import android.net.NetworkCapabilities; //导入方法依赖的package包/类
/**
* Returns true if {@code network} can provide Internet access. Can be used to
* ignore specialized networks (e.g. IMS, FOTA).
*/
@SuppressLint("NewApi")
boolean hasInternetCapability(Network network) {
if (connectivityManager == null) {
return false;
}
final NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
}
示例5: a
import android.net.NetworkCapabilities; //导入方法依赖的package包/类
@SuppressLint({"NewApi"})
public final boolean a(Network paramNetwork)
{
NetworkCapabilities localNetworkCapabilities = this.a.getNetworkCapabilities(paramNetwork);
return (localNetworkCapabilities != null) && (localNetworkCapabilities.hasCapability(12));
}