Android 11 adds functionality to support 5G in your apps. This topic covers the functionality and gives you an overview of how adding 5G-specific functionality to your app can improve the user experience.

## Build for 5G

When deciding how to engage with 5G, think about what types of experiences you are trying to build. Some ways that 5G can enhance your app include:

- Automatically make current experiences faster and better because of the speed and latency improvements of 5G.
- Up-level the user experience, such as by showing 4k video or downloading higher-resolution game assets.
- After confirming that the increased data usage won't cost the user, include experiences normally only provided over Wi-Fi, such as proactively download content typically reserved for unmetered Wi-Fi.
- Provide experiences unique to 5G that work only with high speeds and low latency.

## 5G functionality

Android 11 introduces the following functionality changes and enhancements:

- [Meteredness](https://developer.android.com/about/versions/11/features/5g#meteredness)
- [5G detection](https://developer.android.com/about/versions/11/features/5g#detection)
- [Bandwidth estimation](https://developer.android.com/about/versions/11/features/5g#estimator)

### Check meteredness

The[`NET_CAPABILITY_TEMPORARILY_NOT_METERED`](https://developer.android.com/reference/android/net/NetworkCapabilities#NET_CAPABILITY_TEMPORARILY_NOT_METERED)is a capability added in Android 11 that tells you if the network you are using is unmetered based on information provided by cellular carriers.

The new flag is used alongside[`NET_CAPABILITY_NOT_METERED`](https://developer.android.com/reference/android/net/NetworkCapabilities#NET_CAPABILITY_NOT_METERED). The existing flag indicates if a network is*always*unmetered, and applies to both Wi-Fi and cellular connections.

The difference between the two flags is`NET_CAPABILITY_TEMPORARILY_NOT_METERED`may change without the network type changing. Apps that target Android 11 can use the`NET_CAPABILITY_TEMPORARILY_NOT_METERED`flag. On devices running on Android 9 and lower, the OS will not report on the flag. For apps running on Android 10, this flag may be available, depending on the device it is running on.

Once you've determined that the current network is temporarily or permanently unmetered, you can display higher-resolution content (such as 4k video), upload logs, back up files, and proactively download content.

The following sections cover the steps to add meteredness-checking to your app.

#### Register a network callback

Register for a network callback using[`ConnectivityManager.registerDefaultNetworkCallback()`](https://developer.android.com/reference/android/net/ConnectivityManager#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback))to hear when`NetworkCapabilities`change. You can detect changes to`NetworkCapabilities`by overriding the[`onCapabilitiesChanged()`](https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities))method in your`NetworkCallback`.

`registerDefaultNetworkCallback()`causes the registered callback to trigger immediately when registered, giving the app info about the current state. Future callbacks are critical for the apps to take appropriate action when the state is changing from unmetered to metered or the other way around.

#### Check for meteredness

Use the`NetworkCapabilites`object that you receive in a network callback to check the output of the following code:  

### Kotlin

```kotlin
NetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) ||
  NetworkCapabilities.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED)
```

### Java

```java
NetworkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) ||
  NetworkCapabilities.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED)
```

If the value is true, then you can treat the network as unmetered.

#### Additional considerations

When working with this functionality, keep the following in mind:

- Using the`NET_CAPABILITY_TEMPORARILY_NOT_METERED`flag requires that you compiled your app against the Android 11 SDK.

- The`NET_CAPABILITY_NOT_METERED`capability is permanent on a network. A network with this capability will disconnect automatically if it loses the capability (becomes metered).

- In contrast,`NET_CAPABILITY_TEMPORARILY_NOT_METERED`may change on a network*without disconnecting* . Therefore, apps must listen for the`onCapabilitiesChanged()`callback to handle when the network returns to its metered status (loses the`NET_CAPABILITY_TEMPORARILY_NOT_METERED`capability).

- A network can't have both`NET_CAPABILITY_NOT_METERED`and`NET_CAPABILITY_TEMPORARILY_NOT_METERED`at the same time.

### 5G detection

Starting in Android 11, you can detect if the device is connected to a 5G network using a callback-based API call. You can check for whether the connection is a 5G NR (standalone) or NSA (nonstandalone) network.
| **Note:** While you can detect if you are connected to a 5G network, you cannot assume meteredness, connection speed, nor bandwidth from this signal.

Some uses for this API call may include:

- Displaying 5G branding in your app to highlight that you're offering a unique 5G experience.

- Activating a unique 5G experience in the app only when on a 5G network. You should pair this status check with[checking for meteredness](https://developer.android.com/about/versions/11/features/5g#meteredness).

- Keeping track of 5G connections for analytics purposes.

To test 5G detection without a 5G device, you can use features[added to the Android SDK emulator](https://developer.android.com/about/versions/11/behavior-changes-all#emulator-5g).

#### Detect 5G

Call[`TelephonyManager.listen()`](https://developer.android.com/reference/android/telephony/TelephonyManager#listen(android.telephony.PhoneStateListener,%20int)), passing in[`LISTEN_DISPLAY_INFO_CHANGED`](https://developer.android.com/reference/android/telephony/PhoneStateListener#LISTEN_DISPLAY_INFO_CHANGED), to determine if the user has a 5G network connection. Override the[`onDisplayInfoChanged()`](https://developer.android.com/reference/android/telephony/PhoneStateListener#onDisplayInfoChanged(android.telephony.TelephonyDisplayInfo))method to determine the type of network used for display purposes. One exception is that if the carrier opts to show*5G* as the RAT for their mmWave network,`OVERRIDE_NETWORK_TYPE_NR_NSA`is returned.

The following table shows the networks that correspond to the values:

|                                                                          **Return type**                                                                          |            **Network**             |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|
| [`OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO`](https://developer.android.com/reference/android/telephony/TelephonyDisplayInfo#OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO) | Advanced pro LTE (5Ge)             |
| [`OVERRIDE_NETWORK_TYPE_NR_NSA`](https://developer.android.com/reference/android/telephony/TelephonyDisplayInfo#OVERRIDE_NETWORK_TYPE_NR_NSA)                     | NR (5G) for 5G Sub-6 networks      |
| [`OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE`](https://developer.android.com/reference/android/telephony/TelephonyDisplayInfo#OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE)       | (5G+/5G UW) for 5G mmWave networks |

| **Note:** Your app must have the[`READ_PHONE_STATE`](https://developer.android.com/reference/android/Manifest.permission#READ_PHONE_STATE)permission in order to use this API.

### Bandwidth estimation

Bandwidth estimation uses the`NetworkCapabilities`object that you work with when determining meteredness. You can get bandwidth estimates using that object.

The reliability and accuracy of the bandwidth estimation methods[`getLinkDownstreamBandwidthKbps()`](https://developer.android.com/reference/android/net/NetworkCapabilities#getLinkDownstreamBandwidthKbps())and[`getLinkUpstreamBandwidthKbps()`](https://developer.android.com/reference/android/net/NetworkCapabilities#getLinkUpstreamBandwidthKbps())improve in Android 11 due to upgrades to framework support and platform/modem bug fixes to accommodate 5G.

Bandwidth defaults provide guidance on app start-up only. This should help you with the "start-up on idle" scenario. Your app should measure what it sees once your users have started engaging with the app and adjust its streaming behavior dynamically. For example, you may choose the resolution of video to provide based on the bandwidth estimation at startup. Continue checking the estimates as your users use the app; as their connection type and strength changes, adjust your app's behavior accordingly.
| **Note:** Bandwidth estimates, alone, cannot tell you if the user is on 5G or not. To determine that, see[5G detection](https://developer.android.com/about/versions/11/features/5g#detection).