当前位置: 首页>>代码示例>>Java>>正文


Java NetworkRequest类代码示例

本文整理汇总了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();
}
 
开发者ID:datalink747,项目名称:Rx_java2_soussidev,代码行数:22,代码来源:MarshmallowNetworkObservingStrategy.java

示例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();
}
 
开发者ID:datalink747,项目名称:Rx_java2_soussidev,代码行数:17,代码来源:LollipopNetworkObservingStrategy.java

示例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);
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:27,代码来源:NetworkChangeNotifierTest.java

示例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();
                        }
                    }
                });
    }
}
 
开发者ID:customerly,项目名称:Customerly-Android-SDK,代码行数:26,代码来源:IU_NetworkReceiver.java

示例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();
        }
      });
}
 
开发者ID:google,项目名称:samba-documents-provider,代码行数:16,代码来源:SambaProviderApplication.java

示例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;
    }
}
 
开发者ID:liulinbo,项目名称:Amumu,代码行数:24,代码来源:HomeActivity.java

示例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;
    }
}
 
开发者ID:yongjhih,项目名称:android-proguards,代码行数:26,代码来源:HomeActivity.java

示例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);
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:38,代码来源:NetworkChangeNotifierAutoDetect.java

示例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;
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:16,代码来源:NetworkChangeNotifierTest.java

示例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);
}
 
开发者ID:wzx54321,项目名称:XinFramework,代码行数:6,代码来源:NetWorkNotify.java

示例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);
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:10,代码来源:NetworkMonitorAutoDetect.java

示例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);
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:9,代码来源:NetworkMonitorAutoDetect.java

示例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);
}
 
开发者ID:androidthings,项目名称:sample-lowpan,代码行数:18,代码来源:TransmitterActivity.java

示例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);
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:9,代码来源:MarshmallowNetworkObservingStrategy.java

示例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());
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:8,代码来源:ShadowConnectivityManagerWithCallback.java


注:本文中的android.net.NetworkRequest类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。