Register now for Android Dev Summit 2019!

ConnectivityManagerCompat

class ConnectivityManagerCompat
kotlin.Any
   ↳ androidx.core.net.ConnectivityManagerCompat

Helper for accessing features in ConnectivityManager.

Summary

Constants

static Int

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

static Int

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

static Int

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

Public methods

static NetworkInfo?
getNetworkInfoFromBroadcast(@NonNull cm: ConnectivityManager, @NonNull intent: Intent)

Return the NetworkInfo that caused the given ConnectivityManager#CONNECTIVITY_ACTION broadcast.

static Int

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

static Boolean

Returns if the currently active data network is metered.

Constants

RESTRICT_BACKGROUND_STATUS_DISABLED

static val RESTRICT_BACKGROUND_STATUS_DISABLED: Int

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

Value: 1

RESTRICT_BACKGROUND_STATUS_ENABLED

static val RESTRICT_BACKGROUND_STATUS_ENABLED: Int

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.

Value: 3

RESTRICT_BACKGROUND_STATUS_WHITELISTED

static val RESTRICT_BACKGROUND_STATUS_WHITELISTED: Int

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.

Value: 2

Public methods

getNetworkInfoFromBroadcast

@Nullable static fun getNetworkInfoFromBroadcast(@NonNull cm: ConnectivityManager, @NonNull intent: Intent): NetworkInfo?

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

getRestrictBackgroundStatus

static fun getRestrictBackgroundStatus(@NonNull cm: ConnectivityManager): Int

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

Return
Int: RESTRICT_BACKGROUND_STATUS_DISABLED, RESTRICT_BACKGROUND_STATUS_ENABLED, or RESTRICT_BACKGROUND_STATUS_WHITELISTED

isActiveNetworkMetered

static fun isActiveNetworkMetered(@NonNull cm: ConnectivityManager): Boolean

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 android.Manifest.permission#ACCESS_NETWORK_STATE.

Return
Boolean: true if large transfers should be avoided, otherwise false.