Example usage for android.net NetworkCapabilities NET_CAPABILITY_INTERNET

List of usage examples for android.net NetworkCapabilities NET_CAPABILITY_INTERNET

Introduction

In this page you can find the example usage for android.net NetworkCapabilities NET_CAPABILITY_INTERNET.

Prototype

int NET_CAPABILITY_INTERNET

To view the source code for android.net NetworkCapabilities NET_CAPABILITY_INTERNET.

Click Source Link

Document

Indicates that this network should be able to reach the internet.

Usage

From source file:org.sleepydragon.rgbclient.NetworkClientFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    LOG.v("onCreate()");
    super.onCreate(savedInstanceState);

    // ensure that this fragment doesn't get destroyed due to configuration changes, as that
    // would cause the network connection to have to be re-established every time
    setRetainInstance(true);/* w w w  .j  a  va  2 s.co m*/

    mHandler = new Handler(new MainHandlerCallback());

    mConnectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
    networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    final NetworkRequest networkRequest = networkRequestBuilder.build();
    mConnectivityManager.registerNetworkCallback(networkRequest, mNetworkConnectionListener);

    mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
    final IntentFilter restartReceiverFilter = new IntentFilter();
    restartReceiverFilter.addAction(Settings.ACTION_SERVER_INFO_CHANGED);
    mLocalBroadcastManager.registerReceiver(mRestartBroadcastReceiver, restartReceiverFilter);

    mLoadSettingsAsyncTask = new LoadSettingsAsyncTask(getActivity());
    mLoadSettingsAsyncTask.execute();
}

From source file:com.julia.android.stockhawk.ui.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    // Register network callbacks
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    networkCallback = new NetworkCallback(this);

    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
    networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    connectivityManager.registerNetworkCallback(networkRequestBuilder.build(), networkCallback);

}

From source file:com.mobilyzer.util.PhoneUtils.java

public void switchNetwork(boolean toWiFi, CountDownLatch latch) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder request = new NetworkRequest.Builder();

    if (toWiFi) {
        request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    } else {/*from ww w. j  a  v a  2  s . c o m*/
        request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    }
    request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    ////      request.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
    //      ConnectivityManager.NetworkCallback connectivityNetworkCallback = new ConnectivityNetworkCallback(latch, cm);
    connectivityNetworkCallback = new ConnectivityNetworkCallback(latch, cm);
    cm.requestNetwork(request.build(), connectivityNetworkCallback);

}

From source file:io.plaidapp.ui.HomeActivity.java

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();
        }/*from w  w  w  . j  a  v  a2  s . c om*/
        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;
    }
}