# Create AIDL client

An AIDL client is an app that binds to an AIDL service, calls API, shows response inside its own views.

Steps:

1. Create a service connection:

   ```kotlin
    private var aidlService: ICalculator? = null
    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            aidlService = ICalculator.Stub.asInterface(service)
            Toast.makeText(applicationContext, "Service Connected", Toast.LENGTH_LONG).show()
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            aidlService = null
            Toast.makeText(applicationContext, "Service Disconnected", Toast.LENGTH_LONG).show()
        }
    }
   ```
2. Connect to AIDL service via Binders.

   ```kotlin
    val serviceIntent = Intent()
    serviceIntent.component = ComponentName(BuildConfig.AIDL_PACKAGE, BuildConfig.AIDL_SERVICE)
    bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE)
   ```
3. Call methods of AIDL after successful connection:

   ```kotlin
    aidlService?.add(2, 2)?.let {
        Toast.makeText(
            applicationContext,
            String.format(Locale.getDefault(), "Sum is: %d", it),
            Toast.LENGTH_LONG
        ).show()
    }
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/_/android-tips/aidl/simple-aidl-communication/create-aidl-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
