# ConnectivityManagerCompat

# ConnectivityManagerCompat

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/net/ConnectivityManagerCompat.java+class:androidx.core.net.ConnectivityManagerCompat)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat "View this page in Java")

<br />

```
class ConnectivityManagerCompat
```

<br />

*** ** * ** ***

Helper for accessing features in[ConnectivityManager](https://developer.android.com/reference/android/net/ConnectivityManager.html).

## Summary

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [RESTRICT_BACKGROUND_STATUS_DISABLED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_DISABLED())` = 1` Device is not restricting metered network activity while application is running on background.                                            |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [RESTRICT_BACKGROUND_STATUS_ENABLED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_ENABLED())` = 3` Device is restricting metered network activity while application is running on background.                                                  |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [RESTRICT_BACKGROUND_STATUS_WHITELISTED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_WHITELISTED())` = 2` Device is restricting metered network activity while application is running on background, but application is allowed to bypass it. |

|                                         ### Public functions                                         |
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[NetworkInfo](https://developer.android.com/reference/android/net/NetworkInfo.html)`?` | `@`[RequiresPermission](https://developer.android.com/reference/kotlin/androidx/annotation/RequiresPermission)`(value = `[Manifest.permission.ACCESS_NETWORK_STATE](https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE)`)` [getNetworkInfoFromBroadcast](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#getNetworkInfoFromBroadcast(android.net.ConnectivityManager,android.content.Intent))`(cm: `[ConnectivityManager](https://developer.android.com/reference/android/net/ConnectivityManager.html)`, intent: `[Intent](https://developer.android.com/reference/android/content/Intent.html)`)` Return the[NetworkInfo](https://developer.android.com/reference/android/net/NetworkInfo.html)that caused the given[CONNECTIVITY_ACTION](https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION())broadcast. |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)            | [getRestrictBackgroundStatus](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#getRestrictBackgroundStatus(android.net.ConnectivityManager))`(cm: `[ConnectivityManager](https://developer.android.com/reference/android/net/ConnectivityManager.html)`)` Determines if the calling application is subject to metered network restrictions while running on background.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| `java-static `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)    | `@`[ReplaceWith](https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith)`(expression = "cm.isActiveNetworkMetered()")` `@`[RequiresPermission](https://developer.android.com/reference/kotlin/androidx/annotation/RequiresPermission)`(value = `[Manifest.permission.ACCESS_NETWORK_STATE](https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE)`)` ~~[isActiveNetworkMetered](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#isActiveNetworkMetered(android.net.ConnectivityManager))~~`(cm: `[ConnectivityManager](https://developer.android.com/reference/android/net/ConnectivityManager.html)`)` **This function is deprecated.** Call[isActiveNetworkMetered](https://developer.android.com/reference/android/net/ConnectivityManager.html#isActiveNetworkMetered())directly. <br />                                         |

## Constants

### RESTRICT_BACKGROUND_STATUS_DISABLED

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

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

Return the[NetworkInfo](https://developer.android.com/reference/android/net/NetworkInfo.html)that caused the given[CONNECTIVITY_ACTION](https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION())broadcast. This obtains the current state from[ConnectivityManager](https://developer.android.com/reference/android/net/ConnectivityManager.html)instead of using the potentially-stale value from[EXTRA_NETWORK_INFO](https://developer.android.com/reference/android/net/ConnectivityManager.html#EXTRA_NETWORK_INFO()). May be`null`.  

### getRestrictBackgroundStatus

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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.  

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [RESTRICT_BACKGROUND_STATUS_DISABLED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_DISABLED()),[RESTRICT_BACKGROUND_STATUS_ENABLED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_ENABLED()), or[RESTRICT_BACKGROUND_STATUS_WHITELISTED](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_WHITELISTED()) |

### isActiveNetworkMetered

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  
Deprecated in[1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)  

```
@ReplaceWith(expression = "cm.isActiveNetworkMetered()")
@RequiresPermission(value = Manifest.permission.ACCESS_NETWORK_STATE)
java-static fun [isActiveNetworkMetered](https://developer.android.com/reference/kotlin/androidx/core/net/ConnectivityManagerCompat#isActiveNetworkMetered(android.net.ConnectivityManager))(cm: ConnectivityManager): Boolean
```
| **This function is deprecated.**   
|
| Call[isActiveNetworkMetered](https://developer.android.com/reference/android/net/ConnectivityManager.html#isActiveNetworkMetered())directly.

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](https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE()).  

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | `true`if large transfers should be avoided, otherwise`false`. |