public boolean isConnectionMetered() { if (mNoNetworkPermission) { return false; } return ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager); }
/** Returns true if the currently active network is unmetered. */ private static boolean isNetworkUnmetered(ConnectivityManager connectivityManager) { return !ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); } }
boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager); if (!metered) { return JobRequest.NetworkType.UNMETERED;
public static boolean isMeteredNetwork(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm)); }
/** * 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 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 (networkInfo.isRoaming()) { return JobRequest.NetworkType.CONNECTED; } else { return JobRequest.NetworkType.NOT_ROAMING; } }