Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/net/SocketKeepalive#nestedclasses) \| [Constants](https://developer.android.com/reference/android/net/SocketKeepalive#constants) \| [Methods](https://developer.android.com/reference/android/net/SocketKeepalive#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/SocketKeepalive#inhmethods)

# SocketKeepalive

*** ** * ** ***


`
public


abstract
class
SocketKeepalive
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/lang/AutoCloseable


`

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

<br />

*** ** * ** ***

Allows applications to request that the system periodically send specific packets on their
behalf, using hardware offload to save battery power.

To request that the system send keepalives, call one of the methods that return a
`https://developer.android.com/reference/android/net/SocketKeepalive` object, such as `https://developer.android.com/reference/android/net/ConnectivityManager#createSocketKeepalive(android.net.Network,%20android.net.IpSecManager.UdpEncapsulationSocket,%20java.net.InetAddress,%20java.net.InetAddress,%20java.util.concurrent.Executor,%20android.net.SocketKeepalive.Callback)`,
passing in a non-null callback. If the `https://developer.android.com/reference/android/net/SocketKeepalive` is successfully
started, the callback's `onStarted` method will be called. If an error occurs,
`onError` will be called, specifying one of the `ERROR_*` constants in this
class.

To stop an existing keepalive, call `https://developer.android.com/reference/android/net/SocketKeepalive#stop()`. The system will call
`https://developer.android.com/reference/android/net/SocketKeepalive.Callback#onStopped()` if the operation was successful or
`https://developer.android.com/reference/android/net/SocketKeepalive.Callback#onError(int)` if an error occurred.

For cellular, the device MUST support at least 1 keepalive slot.

For WiFi, the device SHOULD support keepalive offload. If it does not, it MUST reply with
`https://developer.android.com/reference/android/net/SocketKeepalive.Callback#onError(int)` with `ERROR_UNSUPPORTED` to any keepalive offload
request. If it does, it MUST support at least 3 concurrent keepalive slots.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/net/SocketKeepalive.Callback` The callback which app can use to learn the status changes of `https://developer.android.com/reference/android/net/SocketKeepalive`. |

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_HARDWARE_ERROR` There was a hardware error. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INSUFFICIENT_RESOURCES` Resources are insufficient (e.g. all hardware slots are in use). |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_INTERVAL` The interval is invalid (e.g. too short). |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_IP_ADDRESS` The invalid IP addresses. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_LENGTH` The length is invalid (e.g. too long). |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_NETWORK` The invalid network. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_PORT` The port is invalid. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_SOCKET` The socket is invalid. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_SOCKET_NOT_IDLE` The socket is not idle. |
| `int` | `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_UNSUPPORTED` The request is unsupported. |

| ### Public methods ||
|---|---|
| ` final void` | ` https://developer.android.com/reference/android/net/SocketKeepalive#close()() ` Deactivate this `https://developer.android.com/reference/android/net/SocketKeepalive` and free allocated resources. |
| ` final void` | ` https://developer.android.com/reference/android/net/SocketKeepalive#start(int)(int intervalSec) ` Request that keepalive be started with the given `intervalSec`. |
| ` final void` | ` https://developer.android.com/reference/android/net/SocketKeepalive#stop()() ` Requests that keepalive be stopped. |

| ### 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*. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Constants

### ERROR_HARDWARE_ERROR

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

```
public static final int ERROR_HARDWARE_ERROR
```

There was a hardware error.

<br />

Constant Value:

-31
(0xffffffe1)


### ERROR_INSUFFICIENT_RESOURCES

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

```
public static final int ERROR_INSUFFICIENT_RESOURCES
```

Resources are insufficient (e.g. all hardware slots are in use).

<br />

Constant Value:

-32
(0xffffffe0)


### ERROR_INVALID_INTERVAL

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

```
public static final int ERROR_INVALID_INTERVAL
```

The interval is invalid (e.g. too short).

<br />

Constant Value:

-24
(0xffffffe8)


### ERROR_INVALID_IP_ADDRESS

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

```
public static final int ERROR_INVALID_IP_ADDRESS
```

The invalid IP addresses. Indicates the specified IP addresses are invalid.
For example, the specified source IP address is not configured on the
specified `Network`.

<br />

Constant Value:

-21
(0xffffffeb)


### ERROR_INVALID_LENGTH

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

```
public static final int ERROR_INVALID_LENGTH
```

The length is invalid (e.g. too long).

<br />

Constant Value:

-23
(0xffffffe9)


### ERROR_INVALID_NETWORK

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

```
public static final int ERROR_INVALID_NETWORK
```

The invalid network. It indicates the specified `Network` is not connected.

<br />

Constant Value:

-20
(0xffffffec)


### ERROR_INVALID_PORT

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

```
public static final int ERROR_INVALID_PORT
```

The port is invalid.

<br />

Constant Value:

-22
(0xffffffea)


### ERROR_INVALID_SOCKET

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

```
public static final int ERROR_INVALID_SOCKET
```

The socket is invalid.

<br />

Constant Value:

-25
(0xffffffe7)


### ERROR_SOCKET_NOT_IDLE

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

```
public static final int ERROR_SOCKET_NOT_IDLE
```

The socket is not idle.

<br />

Constant Value:

-26
(0xffffffe6)


### ERROR_UNSUPPORTED

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

```
public static final int ERROR_UNSUPPORTED
```

The request is unsupported.

<br />

Constant Value:

-30
(0xffffffe2)


## Public methods

### close

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

```
public final void close ()
```

Deactivate this `https://developer.android.com/reference/android/net/SocketKeepalive` and free allocated resources. The instance won't be
usable again if `close()` is called.

<br />

### start

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

```
public final void start (int intervalSec)
```

Request that keepalive be started with the given `intervalSec`.

See `https://developer.android.com/reference/android/net/SocketKeepalive`. If the remote binder dies, or the binder call throws an
exception when invoking start or stop of the `https://developer.android.com/reference/android/net/SocketKeepalive`, a
`https://developer.android.com/reference/java/lang/RuntimeException` caused by a `https://developer.android.com/reference/android/os/RemoteException` will be thrown into the
`https://developer.android.com/reference/java/util/concurrent/Executor`. This is typically not important to catch because the remote party is
the system, so if it is not in shape to communicate through binder the system is going
down anyway. If the caller still cares, it can use a custom `https://developer.android.com/reference/java/util/concurrent/Executor` to catch the
`https://developer.android.com/reference/java/lang/RuntimeException`.

<br />

| Parameters ||
|---|---|
| `intervalSec` | `int`: The target interval in seconds between keepalive packet transmissions. The interval should be between 10 seconds and 3600 seconds, otherwise `https://developer.android.com/reference/android/net/SocketKeepalive#ERROR_INVALID_INTERVAL` will be returned. Value is between `https://developer.android.com/` and `https://developer.android.com/` inclusive <br /> |

### stop

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

```
public final void stop ()
```

Requests that keepalive be stopped. The application must wait for `https://developer.android.com/reference/android/net/SocketKeepalive.Callback#onStopped()`
before using the object. See `https://developer.android.com/reference/android/net/SocketKeepalive`.

<br />