SocketKeepalive
  public
  
  
  abstract
  class
  SocketKeepalive
  
    extends Object
  
  
  
  
  
      implements
      
        AutoCloseable
      
  
  
| java.lang.Object | |
| ↳ | android.net.SocketKeepalive | 
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
 SocketKeepalive object, such as ConnectivityManager.createSocketKeepalive,
 passing in a non-null callback. If the 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 SocketKeepalive.stop. The system will call
 SocketKeepalive.Callback.onStopped if the operation was successful or
 SocketKeepalive.Callback.onError 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
 SocketKeepalive.Callback.onError with ERROR_UNSUPPORTED to any keepalive offload
 request. If it does, it MUST support at least 3 concurrent keepalive slots.
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        class | SocketKeepalive.CallbackThe callback which app can use to learn the status changes of  | 
| Constants | |
|---|---|
| int | ERROR_HARDWARE_ERRORThere was a hardware error. | 
| int | ERROR_INSUFFICIENT_RESOURCESResources are insufficient (e.g. all hardware slots are in use). | 
| int | ERROR_INVALID_INTERVALThe interval is invalid (e.g. too short). | 
| int | ERROR_INVALID_IP_ADDRESSThe invalid IP addresses. | 
| int | ERROR_INVALID_LENGTHThe length is invalid (e.g. too long). | 
| int | ERROR_INVALID_NETWORKThe invalid network. | 
| int | ERROR_INVALID_PORTThe port is invalid. | 
| int | ERROR_INVALID_SOCKETThe socket is invalid. | 
| int | ERROR_SOCKET_NOT_IDLEThe socket is not idle. | 
| int | ERROR_UNSUPPORTEDThe request is unsupported. | 
| Public methods | |
|---|---|
| 
        
        
        
        final
        
        void | 
      close()
      Deactivate this  | 
| 
        
        
        
        final
        
        void | 
      start(int intervalSec)
      Request that keepalive be started with the given  | 
| 
        
        
        
        final
        
        void | 
      stop()
      Requests that keepalive be stopped. | 
| Inherited methods | |
|---|---|
Constants
ERROR_HARDWARE_ERROR
public static final int ERROR_HARDWARE_ERROR
There was a hardware error.
Constant Value: -31 (0xffffffe1)
ERROR_INSUFFICIENT_RESOURCES
public static final int ERROR_INSUFFICIENT_RESOURCES
Resources are insufficient (e.g. all hardware slots are in use).
Constant Value: -32 (0xffffffe0)
ERROR_INVALID_INTERVAL
public static final int ERROR_INVALID_INTERVAL
The interval is invalid (e.g. too short).
Constant Value: -24 (0xffffffe8)
ERROR_INVALID_IP_ADDRESS
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.
Constant Value: -21 (0xffffffeb)
ERROR_INVALID_LENGTH
public static final int ERROR_INVALID_LENGTH
The length is invalid (e.g. too long).
Constant Value: -23 (0xffffffe9)
ERROR_INVALID_NETWORK
public static final int ERROR_INVALID_NETWORK
The invalid network. It indicates the specified Network is not connected.
Constant Value: -20 (0xffffffec)
ERROR_INVALID_PORT
public static final int ERROR_INVALID_PORT
The port is invalid.
Constant Value: -22 (0xffffffea)
ERROR_INVALID_SOCKET
public static final int ERROR_INVALID_SOCKET
The socket is invalid.
Constant Value: -25 (0xffffffe7)
ERROR_SOCKET_NOT_IDLE
public static final int ERROR_SOCKET_NOT_IDLE
The socket is not idle.
Constant Value: -26 (0xffffffe6)
ERROR_UNSUPPORTED
public static final int ERROR_UNSUPPORTED
The request is unsupported.
Constant Value: -30 (0xffffffe2)
Public methods
close
public final void close ()
Deactivate this SocketKeepalive and free allocated resources. The instance won't be
 usable again if close() is called.
start
public final void start (int intervalSec)
Request that keepalive be started with the given intervalSec.
 See SocketKeepalive. If the remote binder dies, or the binder call throws an
 exception when invoking start or stop of the SocketKeepalive, a
 RuntimeException caused by a RemoteException will be thrown into the
 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 Executor to catch the
 RuntimeException.
| Parameters | |
|---|---|
| intervalSec | int: The target interval in seconds between keepalive packet transmissions.
                    The interval should be between 10 seconds and 3600 seconds, otherwiseERROR_INVALID_INTERVALwill be returned.
 Value is betweenERROR(/android.net.SocketKeepalive#MIN_INTERVAL_SEC)andERROR(/android.net.SocketKeepalive#MAX_INTERVAL_SEC)inclusive | 
stop
public final void stop ()
Requests that keepalive be stopped. The application must wait for Callback.onStopped
 before using the object. See SocketKeepalive.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-08-20 UTC.
