# 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)

*** ** * ** ***


```
public final class ConnectivityManagerCompat
```

<br />

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/net/ConnectivityManager.html`.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_DISABLED() = 1` Device is not restricting metered network activity while application is running on background. |
| `static final int` | `https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_ENABLED() = 3` Device is restricting metered network activity while application is running on background. |
| `static final int` | `https://developer.android.com/reference/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 methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/net/NetworkInfo.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresPermission(value = https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE) https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#getNetworkInfoFromBroadcast(android.net.ConnectivityManager,android.content.Intent)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/ConnectivityManager.html cm, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html intent )` Return the `https://developer.android.com/reference/android/net/NetworkInfo.html` that caused the given `https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION` broadcast. |
| `static int` | `https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#getRestrictBackgroundStatus(android.net.ConnectivityManager)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/ConnectivityManager.html cm)` Determines if the calling application is subject to metered network restrictions while running on background. |
| `static boolean` | `@https://developer.android.com/reference/androidx/annotation/ReplaceWith(expression = "cm.isActiveNetworkMetered()") @https://developer.android.com/reference/androidx/annotation/RequiresPermission(value = https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE) [isActiveNetworkMetered](https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#isActiveNetworkMetered(android.net.ConnectivityManager))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/ConnectivityManager.html cm)` **This method is deprecated.** Call `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)

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

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

Return the `https://developer.android.com/reference/android/net/NetworkInfo.html` that caused the given `https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION` broadcast. This obtains the current state from `https://developer.android.com/reference/android/net/ConnectivityManager.html` instead of using the potentially-stale value from `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)

```
public static int getRestrictBackgroundStatus(@NonNull ConnectivityManager cm)
```

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

| Returns |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_DISABLED()`, `https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#RESTRICT_BACKGROUND_STATUS_ENABLED()`, or `https://developer.android.com/reference/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)
public static boolean [isActiveNetworkMetered](https://developer.android.com/reference/androidx/core/net/ConnectivityManagerCompat#isActiveNetworkMetered(android.net.ConnectivityManager))(@NonNull ConnectivityManager cm)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Call `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 `https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_NETWORK_STATE`.

| Returns |
|---|---|
| `boolean` | `true` if large transfers should be avoided, otherwise `false`. |