ConnectivityManagerCompat

Added in 1.1.0

class ConnectivityManagerCompat


Helper for accessing features in ConnectivityManager.

Summary

Constants

const Int

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

const Int

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

const Int

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

Public functions

java-static NetworkInfo?
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
getNetworkInfoFromBroadcast(cm: ConnectivityManager, intent: Intent)

Return the NetworkInfo that caused the given CONNECTIVITY_ACTION broadcast.

java-static Int

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

java-static Boolean
@ReplaceWith(expression = "cm.isActiveNetworkMetered()")
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
isActiveNetworkMetered(cm: ConnectivityManager)

This function is deprecated.

Call isActiveNetworkMetered directly.

Constants

RESTRICT_BACKGROUND_STATUS_DISABLED

Added in 1.1.0
const val RESTRICT_BACKGROUND_STATUS_DISABLED = 1: Int

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

RESTRICT_BACKGROUND_STATUS_ENABLED

Added in 1.1.0
const val RESTRICT_BACKGROUND_STATUS_ENABLED = 3: 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.

RESTRICT_BACKGROUND_STATUS_WHITELISTED

Added in 1.1.0
const val RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2: 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.

Public functions

getNetworkInfoFromBroadcast

Added in 1.1.0
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
java-static fun getNetworkInfoFromBroadcast(cm: ConnectivityManager, intent: Intent): NetworkInfo?

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
java-static fun getRestrictBackgroundStatus(cm: ConnectivityManager): Int

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)
java-static fun isActiveNetworkMetered(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 ACCESS_NETWORK_STATE.

Returns
Boolean

true if large transfers should be avoided, otherwise false.