ServiceClient

open class ServiceClient<T : IInterface>(stubFactory: (IBinder) -> T) : Closeable(source)

A generic helper for binding to an Android Service via AIDL. Handles connection lifecycle, thread safety for initial binding, and automatic retry for common race conditions.

Parameters

stubFactory

A factory function to convert an IBinder to the interface type.

Type Parameters

T

The type of the AIDL interface.

Constructors

Link copied to clipboard
constructor(stubFactory: (IBinder) -> T)

Properties

Link copied to clipboard
val service: T

Returns the bound service instance. If not currently connected, this will block the current thread until the connection is established.

Link copied to clipboard
var serviceP: T?

The currently bound service instance, or null if not connected.

Functions

Link copied to clipboard
open override fun close()
Link copied to clipboard
suspend fun connect(c: Context, intent: Intent, flags: Int)

Initiates a binding to the service.

Link copied to clipboard
open fun onConnected(service: T)

Called on the main thread when the service is connected.

Link copied to clipboard
open fun onDisconnected()

Called on the main thread when the service connection is lost.

Link copied to clipboard

Blocks the current thread until the service is connected.