本文整理汇总了Java中android.net.NetworkRequest类的典型用法代码示例。如果您正苦于以下问题:Java NetworkRequest类的具体用法?Java NetworkRequest怎么用?Java NetworkRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NetworkRequest类属于android.net包,在下文中一共展示了NetworkRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: observeNetworkConnectivity
import android.net.NetworkRequest; //导入依赖的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: observeNetworkConnectivity
import android.net.NetworkRequest; //导入依赖的package包/类
@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service);
return Observable.create(new ObservableOnSubscribe<Connectivity>() {
@Override public void subscribe(ObservableEmitter<Connectivity> subscriber) throws Exception {
networkCallback = createNetworkCallback(subscriber, context);
final NetworkRequest networkRequest = new NetworkRequest.Builder().build();
manager.registerNetworkCallback(networkRequest, networkCallback);
}
}).doOnDispose(new Action() {
@Override public void run() {
tryToUnregisterCallback(manager);
}
}).startWith(Connectivity.create(context)).distinctUntilChanged();
}
示例3: testConnectivityManagerDelegateDoesNotCrash
import android.net.NetworkRequest; //导入依赖的package包/类
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testConnectivityManagerDelegateDoesNotCrash() {
ConnectivityManagerDelegate delegate =
new ConnectivityManagerDelegate(getInstrumentation().getTargetContext());
delegate.getNetworkState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// getNetworkState(Network) doesn't crash upon invalid Network argument.
Network invalidNetwork = netIdToNetwork(NetId.INVALID);
NetworkState invalidNetworkState = delegate.getNetworkState(invalidNetwork);
assertFalse(invalidNetworkState.isConnected());
assertEquals(-1, invalidNetworkState.getNetworkType());
assertEquals(-1, invalidNetworkState.getNetworkSubType());
Network[] networks = delegate.getAllNetworksUnfiltered();
if (networks.length >= 1) {
delegate.getNetworkState(networks[0]);
}
delegate.getDefaultNetId();
NetworkCallback networkCallback = new NetworkCallback();
NetworkRequest networkRequest = new NetworkRequest.Builder().build();
delegate.registerNetworkCallback(networkRequest, networkCallback);
delegate.unregisterNetworkCallback(networkCallback);
}
}
示例4: registerLollipopNetworkReceiver
import android.net.NetworkRequest; //导入依赖的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();
}
}
});
}
}
示例5: registerNetworkCallback
import android.net.NetworkRequest; //导入依赖的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();
}
});
}
示例6: checkConnectivity
import android.net.NetworkRequest; //导入依赖的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;
}
}
示例7: checkConnectivity
import android.net.NetworkRequest; //导入依赖的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;
}
}
示例8: NetworkChangeNotifierAutoDetect
import android.net.NetworkRequest; //导入依赖的package包/类
/**
* Constructs a NetworkChangeNotifierAutoDetect. Should only be called on UI thread.
* @param policy The RegistrationPolicy which determines when this class should watch
* for network changes (e.g. see (@link RegistrationPolicyAlwaysRegister} and
* {@link RegistrationPolicyApplicationStatus}).
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NetworkChangeNotifierAutoDetect(
Observer observer, Context context, RegistrationPolicy policy) {
// Since BroadcastReceiver is always called back on UI thread, ensure
// running on UI thread so notification logic can be single-threaded.
ThreadUtils.assertOnUiThread();
mObserver = observer;
mContext = context.getApplicationContext();
mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
mWifiManagerDelegate = new WifiManagerDelegate(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mNetworkCallback = new MyNetworkCallback();
mNetworkRequest = new NetworkRequest.Builder()
.addCapability(NET_CAPABILITY_INTERNET)
// Need to hear about VPNs too.
.removeCapability(NET_CAPABILITY_NOT_VPN)
.build();
} else {
mNetworkCallback = null;
mNetworkRequest = null;
}
final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
mConnectionType = getCurrentConnectionType(networkState);
mWifiSSID = getCurrentWifiSSID(networkState);
mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
mMaxBandwidthConnectionType = mConnectionType;
mIntentFilter =
new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
mRegistrationPolicy = policy;
mRegistrationPolicy.init(this);
}
示例9: getCapabilities
import android.net.NetworkRequest; //导入依赖的package包/类
private static NetworkCapabilities getCapabilities(int transport) {
// Create a NetworkRequest with corresponding capabilities
NetworkRequest request = new NetworkRequest.Builder()
.addCapability(NET_CAPABILITY_INTERNET)
.addTransportType(transport)
.build();
// Extract the NetworkCapabilities from the NetworkRequest.
try {
return (NetworkCapabilities) request.getClass()
.getDeclaredField("networkCapabilities")
.get(request);
} catch (NoSuchFieldException | IllegalAccessException e) {
return null;
}
}
示例10: setNetNotifyForNougat
import android.net.NetworkRequest; //导入依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setNetNotifyForNougat(ConnectivityManager.NetworkCallback callback) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.requestNetwork(new NetworkRequest.Builder().build(), callback);
}
示例11: registerNetworkCallback
import android.net.NetworkRequest; //导入依赖的package包/类
/** Only callable on Lollipop and newer releases. */
@SuppressLint("NewApi")
public void registerNetworkCallback(NetworkCallback networkCallback) {
connectivityManager.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(),
networkCallback);
}
示例12: requestMobileNetwork
import android.net.NetworkRequest; //导入依赖的package包/类
/** Only callable on Lollipop and newer releases. */
@SuppressLint("NewApi")
public void requestMobileNetwork(NetworkCallback networkCallback) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
connectivityManager.requestNetwork(builder.build(), networkCallback);
}
示例13: resetNetwork
import android.net.NetworkRequest; //导入依赖的package包/类
/**
* Initializes the network.
*/
private void resetNetwork() {
// Initialize network
onNoNetwork();
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_LOWPAN)
.build();
mBackgroundHandlerThread = new HandlerThread(TAG);
mBackgroundHandlerThread.start();
mHandler = new Handler(mBackgroundHandlerThread.getLooper());
// Make sure that it is connected to a valid network
mConnectivityManager.registerNetworkCallback(networkRequest,
mNetworkCallback, mUiThreadHandler);
}
示例14: registerNetworkCallback
import android.net.NetworkRequest; //导入依赖的package包/类
private void registerNetworkCallback(ObservableEmitter<RxNetworkInfo> upstream) {
networkCallback = new StrategyNetworkCallback(upstream);
NetworkRequest request =
networkRequest != null ? networkRequest : new NetworkRequest.Builder().build();
connectivityManager.registerNetworkCallback(request, networkCallback);
}
示例15: registerNetworkCallback
import android.net.NetworkRequest; //导入依赖的package包/类
@Implementation(minSdk = LOLLIPOP)
@RequiresApi(LOLLIPOP)
public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
networkCallbacks.add(networkCallback);
// simulate available connection
networkCallback.onAvailable(getActiveNetwork());
}