本文整理汇总了Java中android.net.ConnectivityManager.getNetworkCapabilities方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager.getNetworkCapabilities方法的具体用法?Java ConnectivityManager.getNetworkCapabilities怎么用?Java ConnectivityManager.getNetworkCapabilities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.ConnectivityManager
的用法示例。
在下文中一共展示了ConnectivityManager.getNetworkCapabilities方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hasInternetConnection
import android.net.ConnectivityManager; //导入方法依赖的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: getLP
import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private LinkProperties getLP(ConnectivityManager connMgr, int cap) {
Network nets[] = connMgr.getAllNetworks();
for (Network n: nets) {
LinkProperties lp = connMgr.getLinkProperties(n);
NetworkCapabilities np = connMgr.getNetworkCapabilities(n);
String iname = lp.getInterfaceName();
if (iname != null && np != null) {
//Log.d(TAG, ">>> " + iname + ": " + np.hasTransport(cap));
if (np.hasTransport(cap))
return lp;
}
}
return null;
}
示例3: getCapabilities
import android.net.ConnectivityManager; //导入方法依赖的package包/类
@RequiresApi(LOLLIPOP)
@Nullable
static NetworkCapabilities getCapabilities(@NonNull Network network,
@NonNull ConnectivityManager connectivityManager) {
NetworkCapabilities networkCapabilities = null;
try {
networkCapabilities = connectivityManager.getNetworkCapabilities(network);
} catch (Exception exc) {
logger.log(WARNING,
"Could not retrieve network capabilities from provided network: " + exc.getMessage());
}
return networkCapabilities;
}