Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Constants](https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#constants) \| [Ctors](https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#pubctors) \| [Methods](https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#inhmethods)

# ConnectivityManager.NetworkCallback

*** ** * ** ***

` public static class ConnectivityManager.NetworkCallback `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.net.ConnectivityManager.NetworkCallback |

<br />

*** ** * ** ***

Base class for `NetworkRequest` callbacks. Used for notifications about network changes. Should be extended by applications wanting notifications. A `NetworkCallback` is registered by calling `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)`, `https://developer.android.com/reference/android/net/ConnectivityManager#registerNetworkCallback(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)`, or `https://developer.android.com/reference/android/net/ConnectivityManager#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)`. A `NetworkCallback` is unregistered by calling `https://developer.android.com/reference/android/net/ConnectivityManager#unregisterNetworkCallback(android.net.ConnectivityManager.NetworkCallback)`. A `NetworkCallback` should be registered at most once at any time. A `NetworkCallback` that has been unregistered can be registered again.

## Summary

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#FLAG_INCLUDE_LOCATION_INFO` Inclusion of this flag means location-sensitive redaction requests keeping location info. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#NetworkCallback()() ` |
| ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#NetworkCallback(int)(int flags) ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network)(https://developer.android.com/reference/android/net/Network network) ` Called when the framework connects and has declared a new network ready for use. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onBlockedStatusChanged(android.net.Network,%20boolean)(https://developer.android.com/reference/android/net/Network network, boolean blocked) ` Called when access to the specified network is blocked or unblocked. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities)(https://developer.android.com/reference/android/net/Network network, https://developer.android.com/reference/android/net/NetworkCapabilities networkCapabilities) ` Called when the network corresponding to this request changes capabilities but still satisfies the requested criteria. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLinkPropertiesChanged(android.net.Network,%20android.net.LinkProperties)(https://developer.android.com/reference/android/net/Network network, https://developer.android.com/reference/android/net/LinkProperties linkProperties) ` Called when the network corresponding to this request changes `https://developer.android.com/reference/android/net/LinkProperties`. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLosing(android.net.Network,%20int)(https://developer.android.com/reference/android/net/Network network, int maxMsToLive) ` Called when the network is about to be lost, typically because there are no outstanding requests left for it. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLost(android.net.Network)(https://developer.android.com/reference/android/net/Network network) ` Called when a network disconnects or otherwise no longer satisfies this request or callback. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onReserved(android.net.NetworkCapabilities)(https://developer.android.com/reference/android/net/NetworkCapabilities networkCapabilities) ` Called when a network is reserved. |
| ` void` | ` https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onUnavailable()() ` If the callback was registered with one of the `requestNetwork` methods, this will be called if no network is found within the timeout specified in `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback,%20int)` call or if the requested network request cannot be fulfilled (whether or not a timeout was specified). |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Constants

### FLAG_INCLUDE_LOCATION_INFO

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int FLAG_INCLUDE_LOCATION_INFO
```

Inclusion of this flag means location-sensitive redaction requests keeping location info. Some objects like `https://developer.android.com/reference/android/net/NetworkCapabilities` may contain location-sensitive information. Prior to Android 12, this information is always returned to apps holding the appropriate permission, possibly noting that the app has used location.

In Android 12 and above, by default the sent objects do not contain any location information, even if the app holds the necessary permissions, and the system does not take note of location usage by the app. Apps can request that location information is included, in which case the system will check location permission and the location toggle state, and take note of location usage by the app if any such information is returned. Use this flag to include any location sensitive data in `https://developer.android.com/reference/android/net/NetworkCapabilities` sent via `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities)`.

These include:
- Some transport info instances (retrieved via `https://developer.android.com/reference/android/net/NetworkCapabilities#getTransportInfo()`) like `https://developer.android.com/reference/android/net/wifi/WifiInfo` contain location sensitive information.
- OwnerUid (retrieved via `https://developer.android.com/reference/android/net/NetworkCapabilities#getOwnerUid()` is location sensitive for wifi suggestor apps (i.e using `https://developer.android.com/reference/android/net/wifi/WifiNetworkSuggestion`).
- Note:
- Retrieving this location sensitive information (subject to app's location permissions) will be noted by system.
- Without this flag any `https://developer.android.com/reference/android/net/NetworkCapabilities` provided via the callback does not include location sensitive information.

  <br />

  <br />

  Constant Value: 1 (0x00000001)

## Public constructors

### NetworkCallback

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public NetworkCallback ()
```

<br />

<br />

### NetworkCallback

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public NetworkCallback (int flags)
```

<br />

<br />

| Parameters ||
|---|---|
| `flags` | `int`: Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#FLAG_INCLUDE_LOCATION_INFO` |

## Public methods

### onAvailable

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onAvailable (Network network)
```

Called when the framework connects and has declared a new network ready for use.

For callbacks registered with `https://developer.android.com/reference/android/net/ConnectivityManager#registerNetworkCallback(android.net.NetworkRequest,%20android.app.PendingIntent)`, multiple networks may be available at the same time, and onAvailable will be called for each of these as they appear.

For callbacks registered with `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.app.PendingIntent)` and `https://developer.android.com/reference/android/net/ConnectivityManager#registerDefaultNetworkCallback(android.net.ConnectivityManager.NetworkCallback)`, this means the network passed as an argument is the new best network for this request and is now tracked by this callback ; this callback will no longer receive method calls about other networks that may have been passed to this method previously. The previously-best network may have disconnected, or it may still be around and the newly-best network may simply be better.

Starting with `https://developer.android.com/reference/android/os/Build.VERSION_CODES#O`, this will always immediately be followed by a call to `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities)` then by a call to `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLinkPropertiesChanged(android.net.Network,%20android.net.LinkProperties)`, and a call to `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onBlockedStatusChanged(android.net.Network,%20boolean)`.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkCapabilities(android.net.Network)` or `https://developer.android.com/reference/android/net/ConnectivityManager#getLinkProperties(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions (there is no guarantee the objects returned by these methods will be current). Instead, wait for a call to `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities)` and `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLinkPropertiesChanged(android.net.Network,%20android.net.LinkProperties)` whose arguments are guaranteed to be well-ordered with respect to other callbacks.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` of the satisfying network. This value cannot be `null`. <br /> |

### onBlockedStatusChanged

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onBlockedStatusChanged (Network network, 
                boolean blocked)
```

Called when access to the specified network is blocked or unblocked.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkCapabilities(android.net.Network)` or `https://developer.android.com/reference/android/net/ConnectivityManager#getLinkProperties(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions : calling these methods while in a callback may return an outdated or even a null object.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` whose blocked status has changed. This value cannot be `null`. <br /> |
| `blocked` | `boolean`: The blocked status of this `https://developer.android.com/reference/android/net/Network`. <br /> |

### onCapabilitiesChanged

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onCapabilitiesChanged (Network network, 
                NetworkCapabilities networkCapabilities)
```

Called when the network corresponding to this request changes capabilities but still satisfies the requested criteria.

Starting with `https://developer.android.com/reference/android/os/Build.VERSION_CODES#O` this method is guaranteed to be called immediately after `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network)`.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getLinkProperties(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions : calling these methods while in a callback may return an outdated or even a null object.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` whose capabilities have changed. This value cannot be `null`. <br /> |
| `networkCapabilities` | `NetworkCapabilities`: The new `https://developer.android.com/reference/android/net/NetworkCapabilities` for this network. This value cannot be `null`. <br /> |

### onLinkPropertiesChanged

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onLinkPropertiesChanged (Network network, 
                LinkProperties linkProperties)
```

Called when the network corresponding to this request changes `https://developer.android.com/reference/android/net/LinkProperties`.

Starting with `https://developer.android.com/reference/android/os/Build.VERSION_CODES#O` this method is guaranteed to be called immediately after `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network)`.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkCapabilities(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions : calling these methods while in a callback may return an outdated or even a null object.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` whose link properties have changed. This value cannot be `null`. <br /> |
| `linkProperties` | `LinkProperties`: The new `https://developer.android.com/reference/android/net/LinkProperties` for this network. This value cannot be `null`. <br /> |

### onLosing

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onLosing (Network network, 
                int maxMsToLive)
```

Called when the network is about to be lost, typically because there are no outstanding requests left for it. This may be paired with a `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network)` call with the new replacement network for graceful handover. This method is not guaranteed to be called before `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onLost(android.net.Network)` is called, for example in case a network is suddenly disconnected.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkCapabilities(android.net.Network)` or `https://developer.android.com/reference/android/net/ConnectivityManager#getLinkProperties(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions ; calling these methods while in a callback may return an outdated or even a null object.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` that is about to be lost. This value cannot be `null`. <br /> |
| `maxMsToLive` | `int`: The time in milliseconds the system intends to keep the network connected for graceful handover; note that the network may still suffer a hard loss at any time. <br /> |

### onLost

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onLost (Network network)
```

Called when a network disconnects or otherwise no longer satisfies this request or callback.

If the callback was registered with requestNetwork() or registerDefaultNetworkCallback(), it will only be invoked against the last network returned by onAvailable() when that network is lost and no other network satisfies the criteria of the request.

If the callback was registered with registerNetworkCallback() it will be called for each network which no longer satisfies the criteria of the callback.

Do NOT call `https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkCapabilities(android.net.Network)` or `https://developer.android.com/reference/android/net/ConnectivityManager#getLinkProperties(android.net.Network)` or other synchronous ConnectivityManager methods in this callback as this is prone to race conditions ; calling these methods while in a callback may return an outdated or even a null object.

<br />

| Parameters ||
|---|---|
| `network` | `Network`: The `https://developer.android.com/reference/android/net/Network` lost. This value cannot be `null`. <br /> |

### onReserved

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in [U Extensions 17](https://developer.android.com/sdkExtensions)

```
public void onReserved (NetworkCapabilities networkCapabilities)
```

Called when a network is reserved. The reservation includes the `https://developer.android.com/reference/android/net/NetworkCapabilities` that uniquely describe the network that was reserved. the caller communicates this information to hardware or software components on or off-device to instruct them to create a network matching this reservation. `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onReserved(android.net.NetworkCapabilities)` is called at most once and is guaranteed to be called before any other callback unless the reservation is unavailable. Once a reservation is made, the reserved `https://developer.android.com/reference/android/net/NetworkCapabilities` will not be updated, and the reservation remains in place until the reserved network connects or `https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback#onUnavailable()` is called.

<br />

| Parameters ||
|---|---|
| `networkCapabilities` | `NetworkCapabilities`: The `https://developer.android.com/reference/android/net/NetworkCapabilities` of the reservation. This value cannot be `null`. <br /> |

### onUnavailable

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onUnavailable ()
```

If the callback was registered with one of the `requestNetwork` methods, this will be called if no network is found within the timeout specified in `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback,%20int)` call or if the requested network request cannot be fulfilled (whether or not a timeout was specified). If the callback was registered when reserving a network, this method indicates that the reservation is removed. It can be called when the reservation is requested, because the system could not satisfy the reservation, or after the reserved network connects. When this callback is invoked the associated `https://developer.android.com/reference/android/net/NetworkRequest` will have already been removed and released, as if `https://developer.android.com/reference/android/net/ConnectivityManager#unregisterNetworkCallback(android.net.ConnectivityManager.NetworkCallback)` had been called.

<br />