NetworkCapabilities
NetworkCapabilities 可以理解为网络能力的标识,类似于Call的Capability. 更加类似于一个工具类。
标志该网络有哪些能力,可以干些什么事,这也就决定了该类有些特定的方法。
/**
* Adds the given capability to this {@code NetworkCapability} instance.
* Multiple capabilities may be applied sequentially. Note that when searching
* for a network to satisfy a request, all capabilities requested must be satisfied.
*
* @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be added.
* @return This NetworkCapabilities instance, to facilitate chaining.
* 给该network添加特定的capability
*/
public NetworkCapabilities addCapability(int capability) {
if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
throw new IllegalArgumentException("NetworkCapability out of range");
}
mNetworkCapabilities |= 1 << capability;
return this;
}
/**
* Tests for the presence of a capabilitity on this instance.
*
* @param capability the {@code NetworkCapabilities.NET_CAPABILITY_*} to be tested for.
* @return {@code true} if set on this instance.
* 判断该网络是否拥有某种特定的网络。
*/
public boolean hasCapability(int capability) {
if (capability < MIN_NET_CAPABILITY || capability > MAX_NET_CAPABILITY) {
return false;
}
return ((mNetworkCapabilities & (1 << capability)) != 0);
}
常见的capabilities 有 NET_CAPABILITY_MMS , NET_CAPABILITY_SUPL ,NET_CAPABILITY_DUN 等
标志网络传输类型,说明是通过哪种方式传递的。
/**
* Gets all the transports set on this {@code NetworkCapability} instance.
*
* @return an array of {@code NetworkCapabilities.TRANSPORT_*} values
* for this instance.
* 获得传输的类型
*/
public int[] getTransportTypes() {
return enumerateBits(mTransportTypes);
}
传输的类型有:
/**
* Indicates this network uses a Cellular transport.
*/
public static final int TRANSPORT_CELLULAR = 0;
/**
* Indicates this network uses a Wi-Fi transport.
*/
public static final int TRANSPORT_WIFI = 1;
/**
* Indicates this network uses a Bluetooth transport.
*/
public static final int TRANSPORT_BLUETOOTH = 2;
/**
* Indicates this network uses an Ethernet transport.
*/
public static final int TRANSPORT_ETHERNET = 3;
/**
* Indicates this network uses a VPN transport.
*/
public static final int TRANSPORT_VPN = 4;
NetworkCapabilities 本身没有太多功能,主要是对网络进行标识。从而供其他模块调用时判断或构建Networkrequest.
private String getImsIface() {
final NetworkState[] states;
try {
states = mConnManager.getAllNetworkState();
} catch (RemoteException e) {
Log.e(this, "getVoiceCallIfaces RemoteException" + e);
return null;
}
if (states != null) {
for (NetworkState state : states) {
//判断链接的network是否有reach 运营商ims的能力
if (state.networkInfo.isConnected() && state.networkCapabilities.hasCapability(
NetworkCapabilities.NET_CAPABILITY_IMS)) {
final boolean isMobile = ConnectivityManager.isNetworkTypeMobile(
state.networkInfo.getType());
final String baseIface = state.linkProperties.getInterfaceName();
if (isMobile)
return baseIface;
}
}
}
return null;
}