Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#pubctors) \| [Methods](https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#inhmethods)

# IkeSession

*** ** * ** ***

` public final class IkeSession `   
` 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.ipsec.ike.IkeSession |

<br />

*** ** * ** ***

This class represents an IKE Session management object that allows for keying and management of `https://developer.android.com/reference/android/net/IpSecTransform`s.

An IKE/Child Session represents an IKE/Child SA as well as its rekeyed successors. A Child Session is bounded by the lifecycle of the IKE Session under which it is set up. Closing an IKE Session implicitly closes any remaining Child Sessions under it.

An IKE procedure is one or multiple IKE message exchanges that are used to create, delete or rekey an IKE Session or Child Session.

This class provides methods for initiating IKE procedures, such as the Creation and Deletion of a Child Session, or the Deletion of the IKE session. All procedures (except for IKE deletion) will be initiated sequentially after IKE Session is set up.
**See also:**

- [RFC 7296, Internet Key Exchange Protocol Version 2 (IKEv2)](https://tools.ietf.org/html/rfc7296)

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#IkeSession(android.content.Context,%20android.net.ipsec.ike.IkeSessionParams,%20android.net.ipsec.ike.ChildSessionParams,%20java.util.concurrent.Executor,%20android.net.ipsec.ike.IkeSessionCallback,%20android.net.ipsec.ike.ChildSessionCallback)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionParams ikeSessionParams, https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionParams firstChildSessionParams, https://developer.android.com/reference/java/util/concurrent/Executor userCbExecutor, https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionCallback ikeSessionCallback, https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback firstChildSessionCallback) ` Constructs a new IKE session. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#close()() ` Close the IKE session gracefully. |
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#closeChildSession(android.net.ipsec.ike.ChildSessionCallback)(https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback childSessionCallback) ` Delete a Child Session. |
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#dump(java.io.PrintWriter)(https://developer.android.com/reference/java/io/PrintWriter pw) ` Dumps the state of `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession` information for the clients |
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#kill()() ` Terminate (forcibly close) the IKE session. |
| ` void` | ` https://developer.android.com/reference/android/net/ipsec/ike/IkeSession#openChildSession(android.net.ipsec.ike.ChildSessionParams,%20android.net.ipsec.ike.ChildSessionCallback)(https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionParams childSessionParams, https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback childSessionCallback) ` Request a new Child Session. |

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

## Public constructors

### IkeSession

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

```
public IkeSession (Context context, 
                IkeSessionParams ikeSessionParams, 
                ChildSessionParams firstChildSessionParams, 
                Executor userCbExecutor, 
                IkeSessionCallback ikeSessionCallback, 
                ChildSessionCallback firstChildSessionCallback)
```

Constructs a new IKE session.

This method will immediately return an instance of `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession` and asynchronously initiate the setup procedure of `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession` as well as its first Child Session. Callers will be notified of these two setup results via the callback arguments.

FEATURE_IPSEC_TUNNELS is required for setting up a tunnel mode Child SA.

<br />

| Parameters ||
|---|---|
| `context` | `Context`: a valid `https://developer.android.com/reference/android/content/Context` instance. This value cannot be `null`. <br /> |
| `ikeSessionParams` | `IkeSessionParams`: the `https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionParams` that contains a set of valid `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession` configurations. This value cannot be `null`. <br /> |
| `firstChildSessionParams` | `ChildSessionParams`: the `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionParams` that contains a set of valid configurations for the first Child Session. This value cannot be `null`. <br /> |
| `userCbExecutor` | `Executor`: the `https://developer.android.com/reference/java/util/concurrent/Executor` upon which all callbacks will be posted. For security and consistency, the callbacks posted to this executor MUST be executed serially and in the order they were posted, as guaranteed by executors such as `https://developer.android.com/reference/java/util/concurrent/Executors#newSingleThreadExecutor()` This value cannot be `null`. <br /> |
| `ikeSessionCallback` | `IkeSessionCallback`: the `https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionCallback` interface to notify callers of state changes within the `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession`. This value cannot be `null`. <br /> |
| `firstChildSessionCallback` | `ChildSessionCallback`: the `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback` interface to notify callers of state changes within the first Child Session. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
|   | an instance of `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession`. <br /> |

## Public methods

### close

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

```
public void close ()
```

Close the IKE session gracefully.

Implements `https://developer.android.com/reference/java/lang/AutoCloseable#close()`

Upon closure, `https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionCallback#onClosed()` or `https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionCallback#onClosedWithException(android.net.ipsec.ike.exceptions.IkeException)` will be fired.

Closing an IKE Session implicitly closes any remaining Child Sessions negotiated under it. Users SHOULD stop all outbound traffic that uses these Child Sessions (`https://developer.android.com/reference/android/net/IpSecTransform` pairs) before calling this method. Otherwise IPsec packets will be dropped due to the lack of a valid `https://developer.android.com/reference/android/net/IpSecTransform`.

Closure of an IKE session will take priority over, and cancel other procedures waiting in the queue (but will wait for ongoing locally initiated procedures to complete). After sending the Delete request, the IKE library will wait until a Delete response is received or retransmission timeout occurs.

<br />

### closeChildSession

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

```
public void closeChildSession (ChildSessionCallback childSessionCallback)
```

Delete a Child Session.

Upon closure, `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback#onClosed()` will be fired.

<br />

| Parameters ||
|---|---|
| `childSessionCallback` | `ChildSessionCallback`: The `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback` instance that uniquely identify the Child Session. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if no Child Session found bound with this callback. |

### dump

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

```
public void dump (PrintWriter pw)
```

Dumps the state of `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession` information for the clients

<br />

| Parameters ||
|---|---|
| `pw` | `PrintWriter`: Print writer. This value cannot be `null`. <br /> |

### finalize

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

```
public void finalize ()
```

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the `finalize` method to dispose of system resources or to perform other cleanup.

The general contract of `finalize` is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The `finalize` method may take any action, including making this object available again to other threads; the usual purpose of `finalize`, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The `finalize` method of class `Object` performs no special action; it simply returns normally. Subclasses of `Object` may override this definition.

The Java programming language does not guarantee which thread will invoke the `finalize` method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the `finalize` method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The `finalize` method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the `finalize` method causes the finalization of this object to be halted, but is otherwise ignored.

<br />

### kill

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

```
public void kill ()
```

Terminate (forcibly close) the IKE session.

Upon closing, `https://developer.android.com/reference/android/net/ipsec/ike/IkeSessionCallback#onClosed()` will be fired.

Closing an IKE Session implicitly closes any remaining Child Sessions negotiated under it. Users SHOULD stop all outbound traffic that uses these Child Sessions (`https://developer.android.com/reference/android/net/IpSecTransform` pairs) before calling this method. Otherwise IPsec packets will be dropped due to the lack of a valid `https://developer.android.com/reference/android/net/IpSecTransform`.

Forcible closure of an IKE session will take priority over, and cancel other procedures waiting in the queue. It will also interrupt any ongoing locally initiated procedure.

<br />

### openChildSession

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

```
public void openChildSession (ChildSessionParams childSessionParams, 
                ChildSessionCallback childSessionCallback)
```

Request a new Child Session.

Users MUST provide a unique `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback` instance for each new Child Session.

Upon setup, `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback#onOpened(android.net.ipsec.ike.ChildSessionConfiguration)` will be fired.

FEATURE_IPSEC_TUNNELS is required for setting up a tunnel mode Child SA.

<br />

| Parameters ||
|---|---|
| `childSessionParams` | `ChildSessionParams`: the `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionParams` that contains the Child Session configurations to negotiate. This value cannot be `null`. <br /> |
| `childSessionCallback` | `ChildSessionCallback`: the `https://developer.android.com/reference/android/net/ipsec/ike/ChildSessionCallback` interface to notify users the state changes of the Child Session. It will be posted to the callback `https://developer.android.com/reference/java/util/concurrent/Executor` of this `https://developer.android.com/reference/android/net/ipsec/ike/IkeSession`. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the ChildSessionCallback is already in use. |