ConnectivityManagerCompat

Added in 1.1.0

public final class ConnectivityManagerCompat


Helper for accessing features in ConnectivityManager.

Summary

Constants

static final int

Device is not restricting metered network activity while application is running on background.

static final int

Device is restricting metered network activity while application is running on background.

static final int

Device is restricting metered network activity while application is running on background, but application is allowed to bypass it.

Public methods

static @Nullable NetworkInfo
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
getNetworkInfoFromBroadcast(
    @NonNull ConnectivityManager cm,
    @NonNull Intent intent
)

Return the NetworkInfo that caused the given CONNECTIVITY_ACTION broadcast.

static int

Determines if the calling application is subject to metered network restrictions while running on background.

static boolean
@ReplaceWith(expression = "cm.isActiveNetworkMetered()")
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
isActiveNetworkMetered(@NonNull ConnectivityManager cm)

This method is deprecated.

Call isActiveNetworkMetered directly.

Constants

RESTRICT_BACKGROUND_STATUS_DISABLED

Added in 1.1.0
public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1

Device is not restricting metered network activity while application is running on background.

RESTRICT_BACKGROUND_STATUS_ENABLED

Added in 1.1.0
public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3

Device is restricting metered network activity while application is running on background.

In this state, application should not try to use the network while running on background, because it would be denied.

RESTRICT_BACKGROUND_STATUS_WHITELISTED

Added in 1.1.0
public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2

Device is restricting metered network activity while application is running on background, but application is allowed to bypass it.

In this state, application should take action to mitigate metered network access. For example, a music streaming application should switch to a low-bandwidth bitrate.

Public methods

getNetworkInfoFromBroadcast

Added in 1.1.0
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
public static @Nullable NetworkInfo getNetworkInfoFromBroadcast(
    @NonNull ConnectivityManager cm,
    @NonNull Intent intent
)

Return the NetworkInfo that caused the given CONNECTIVITY_ACTION broadcast. This obtains the current state from ConnectivityManager instead of using the potentially-stale value from EXTRA_NETWORK_INFO. May be null.

getRestrictBackgroundStatus

Added in 1.1.0
public static int getRestrictBackgroundStatus(@NonNull ConnectivityManager cm)

Determines if the calling application is subject to metered network restrictions while running on background.

isActiveNetworkMetered

Added in 1.1.0
Deprecated in 1.15.0
@ReplaceWith(expression = "cm.isActiveNetworkMetered()")
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
public static boolean isActiveNetworkMetered(@NonNull ConnectivityManager cm)

Returns if the currently active data network is metered. A network is classified as metered when the user is sensitive to heavy data usage on that connection due to monetary costs, data limitations or battery/performance issues. You should check this before doing large data transfers, and warn the user or delay the operation until another network is available.

This method requires the caller to hold the permission ACCESS_NETWORK_STATE.

Returns
boolean

true if large transfers should be avoided, otherwise false.