androidx.core.net.ConnectivityManagerCompat Java Examples

The following examples show how to use androidx.core.net.ConnectivityManagerCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MeteredConnectivityObserver.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@MainThread
MeteredConnectivityObserver(@NonNull Context context, @NonNull LifecycleOwner lifecycleOwner) {
  this.context             = context;
  this.connectivityManager = ServiceUtil.getConnectivityManager(context);
  this.metered             = new MutableLiveData<>();

  this.metered.setValue(ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager));
  lifecycleOwner.getLifecycle().addObserver(this);
}
 
Example #2
Source File: Device.java    From android-job with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the network condition of the device and returns the best type. If the device
 * is connected to a WiFi and mobile network at the same time, then it would assume
 * that the connection is unmetered because of the WiFi connection.
 *
 * @param context Any context, e.g. the application context.
 * @return The current network type of the device.
 */
@NonNull
@SuppressWarnings("deprecation")
public static JobRequest.NetworkType getNetworkType(@NonNull Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo;
    try {
        networkInfo = connectivityManager.getActiveNetworkInfo();
    } catch (Throwable t) {
        return JobRequest.NetworkType.ANY;
    }

    if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
        return JobRequest.NetworkType.ANY;
    }

    boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager);
    if (!metered) {
        return JobRequest.NetworkType.UNMETERED;
    }

    if (isRoaming(connectivityManager, networkInfo)) {
        return JobRequest.NetworkType.CONNECTED;
    } else {
        return JobRequest.NetworkType.NOT_ROAMING;
    }
}
 
Example #3
Source File: MeteredConnectivityObserver.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  metered.postValue(ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager));
}
 
Example #4
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isMeteredNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm));
}
 
Example #5
Source File: Util.java    From InviZible with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isMeteredNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm));
}
 
Example #6
Source File: NetInfoModule.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
public boolean isConnectionMetered() {
    if (mNoNetworkPermission) {
        return false;
    }
    return ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager);
}
 
Example #7
Source File: Util.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isMeteredNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm));
}
 
Example #8
Source File: Util.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isMeteredNetwork(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm));
}