本文整理汇总了Java中android.net.NetworkCapabilities类的典型用法代码示例。如果您正苦于以下问题:Java NetworkCapabilities类的具体用法?Java NetworkCapabilities怎么用?Java NetworkCapabilities使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NetworkCapabilities类属于android.net包,在下文中一共展示了NetworkCapabilities类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: observeNetworkConnectivity
import android.net.NetworkCapabilities; //导入依赖的package包/类
@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service);
networkCallback = createNetworkCallback(context);
registerIdleReceiver(context);
final NetworkRequest request =
new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.build();
manager.registerNetworkCallback(request, networkCallback);
return connectivitySubject.toFlowable(BackpressureStrategy.LATEST).doOnCancel(new Action() {
@Override public void run() {
tryToUnregisterCallback(manager);
tryToUnregisterReceiver(context);
}
}).startWith(Connectivity.create(context)).distinctUntilChanged().toObservable();
}
示例2: 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);
}
示例3: onCapabilitiesChanged
import android.net.NetworkCapabilities; //导入依赖的package包/类
@Override
public void onCapabilitiesChanged(
Network network, NetworkCapabilities networkCapabilities) {
if (ignoreConnectedNetwork(network, networkCapabilities)) {
return;
}
// A capabilities change may indicate the ConnectionType has changed,
// so forward the new ConnectionType along to observer.
final long netId = networkToNetId(network);
final int connectionType =
getCurrentConnectionType(mConnectivityManagerDelegate.getNetworkState(network));
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
mObserver.onNetworkConnect(netId, connectionType);
}
});
}
示例4: hasTransportType
import android.net.NetworkCapabilities; //导入依赖的package包/类
/**
* Determines if any of specified transport types occurred.
*
* @param transportTypes one or many {@code NetworkCapabilities#TRANSPORT_*}
*
* @return {@code true} if any of the given transport types occurred, {@code false} otherwise
*/
public static Predicate<RxNetworkInfo> hasTransportType(final int... transportTypes) {
return new Predicate<RxNetworkInfo>() {
@Override
public boolean test(RxNetworkInfo networkInfo) throws Exception {
final NetworkCapabilities networkCapabilities = networkInfo.getNetworkCapabilities();
if (networkCapabilities != null) {
for (Integer transportType : transportTypes) {
if (networkCapabilities.hasTransport(transportType)) {
return true;
}
}
}
return false;
}
};
}
示例5: registerLollipopNetworkReceiver
import android.net.NetworkCapabilities; //导入依赖的package包/类
public static void registerLollipopNetworkReceiver(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
boolean connected = false;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connected = cm.bindProcessToNetwork(network);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//noinspection deprecation
connected = ConnectivityManager.setProcessDefaultNetwork(network);
}
if(connected) {
Customerly.get().__SOCKET__check();
}
}
});
}
}
示例6: registerNetworkCallback
import android.net.NetworkCapabilities; //导入依赖的package包/类
private void registerNetworkCallback(Context context) {
final ConnectivityManager manager =
(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
manager.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build(),
new NetworkCallback() {
@Override
public void onAvailable(Network network) {
mSambaClient.reset();
}
});
}
示例7: checkConnectivity
import android.net.NetworkCapabilities; //导入依赖的package包/类
private void checkConnectivity() {
final ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
connected = activeNetworkInfo != null && activeNetworkInfo.isConnected();
if (!connected) {
loading.setVisibility(View.GONE);
if (noConnection == null) {
final ViewStub stub = (ViewStub) findViewById(R.id.stub_no_connection);
noConnection = (ImageView) stub.inflate();
}
final AnimatedVectorDrawable avd =
(AnimatedVectorDrawable) getDrawable(R.drawable.avd_no_connection);
noConnection.setImageDrawable(avd);
avd.start();
connectivityManager.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
connectivityCallback);
monitoringConnectivity = true;
}
}
示例8: getDns
import android.net.NetworkCapabilities; //导入依赖的package包/类
@Nonnull
private List<InetAddress> getDns() {
final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
CONNECTIVITY_SERVICE
);
if (connectivityManager == null) {
return Collections.emptyList();
}
return Observable.fromArray(
connectivityManager.getAllNetworks()
).filter(it -> connectivityManager.getNetworkCapabilities(it).hasCapability(
NetworkCapabilities.NET_CAPABILITY_INTERNET
)).map(connectivityManager::getLinkProperties).flatMap(
it -> Observable.fromIterable(it.getDnsServers())
).toList().blockingGet();
}
示例9: checkConnectivity
import android.net.NetworkCapabilities; //导入依赖的package包/类
private void checkConnectivity() {
final ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
connected = activeNetworkInfo != null && activeNetworkInfo.isConnected();
if (!connected) {
loading.setVisibility(View.GONE);
if (noConnection == null) {
final ViewStub stub = (ViewStub) findViewById(R.id.stub_no_connection);
noConnection = (ImageView) stub.inflate();
}
final AnimatedVectorDrawable avd =
(AnimatedVectorDrawable) getDrawable(R.drawable.avd_no_connection);
if (noConnection != null && avd != null) {
noConnection.setImageDrawable(avd);
avd.start();
}
connectivityManager.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build(),
connectivityCallback);
monitoringConnectivity = true;
}
}
示例10: 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);
}
示例11: onCapabilitiesChanged
import android.net.NetworkCapabilities; //导入依赖的package包/类
@Override
public void onCapabilitiesChanged(
Network network, NetworkCapabilities networkCapabilities) {
if (ignoreConnectedNetwork(network, networkCapabilities)) {
return;
}
// A capabilities change may indicate the ConnectionType has changed,
// so forward the new ConnectionType along to observer.
final long netId = networkToNetId(network);
final int connectionType = mConnectivityManagerDelegate.getConnectionType(network);
runOnThread(new Runnable() {
@Override
public void run() {
mObserver.onNetworkConnect(netId, connectionType);
}
});
}
示例12: getLP
import android.net.NetworkCapabilities; //导入依赖的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;
}
示例13: initializeVpnInPlace
import android.net.NetworkCapabilities; //导入依赖的package包/类
void initializeVpnInPlace() {
final Network[] networks = getAllNetworksFiltered(mConnectivityManagerDelegate, null);
mVpnInPlace = null;
// If the filtered list of networks contains just a VPN, then that VPN is in place.
if (networks.length == 1) {
final NetworkCapabilities capabilities =
mConnectivityManagerDelegate.getNetworkCapabilities(networks[0]);
if (capabilities != null && capabilities.hasTransport(TRANSPORT_VPN)) {
mVpnInPlace = networks[0];
}
}
}
示例14: ignoreConnectedInaccessibleVpn
import android.net.NetworkCapabilities; //导入依赖的package包/类
/**
* Should changes to connected network {@code network} be ignored?
* @param network Network to possibly consider ignoring changes to.
* @param capabilities {@code NetworkCapabilities} for {@code network} if known, otherwise
* {@code null}.
* @return {@code true} when either: {@code network} is an inaccessible VPN, or has already
* disconnected.
*/
private boolean ignoreConnectedInaccessibleVpn(
Network network, NetworkCapabilities capabilities) {
// Fetch capabilities if not provided.
if (capabilities == null) {
capabilities = mConnectivityManagerDelegate.getNetworkCapabilities(network);
}
// Ignore inaccessible VPNs as they don't apply to Chrome.
return capabilities == null
|| capabilities.hasTransport(TRANSPORT_VPN)
&& !mConnectivityManagerDelegate.vpnAccessible(network);
}
示例15: onAvailable
import android.net.NetworkCapabilities; //导入依赖的package包/类
@Override
public void onAvailable(Network network) {
final NetworkCapabilities capabilities =
mConnectivityManagerDelegate.getNetworkCapabilities(network);
if (ignoreConnectedNetwork(network, capabilities)) {
return;
}
final boolean makeVpnDefault = capabilities.hasTransport(TRANSPORT_VPN);
if (makeVpnDefault) {
mVpnInPlace = network;
}
final long netId = networkToNetId(network);
final int connectionType =
getCurrentConnectionType(mConnectivityManagerDelegate.getNetworkState(network));
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
mObserver.onNetworkConnect(netId, connectionType);
if (makeVpnDefault) {
// Make VPN the default network.
mObserver.onConnectionTypeChanged(connectionType);
// Purge all other networks as they're inaccessible to Chrome now.
mObserver.purgeActiveNetworkList(new long[] {netId});
}
}
});
}