# Create AIDL client

* [x] Create Callback Implementation

<pre class="language-kotlin"><code class="lang-kotlin"><strong>// callback implemetation
</strong>private val aidlCallback: ICalculatorCallback = object : ICalculatorCallback.Stub() {
    override fun onAdded(result: Int) {
        // do something when callback is received
        Toast.makeText(
            applicationContext,
            String.format(Locale.getDefault(), "Sum is: %d", result),
            Toast.LENGTH_LONG
        ).show()
    }
}
</code></pre>

* [x] Create AILD service connection, and connect to it.

```kotlin
// aild service connection
private var aidlService: ICalculator? = null
private val serviceConnection = object : ServiceConnection {
    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        // get aidl service instance
        aidlService = ICalculator.Stub.asInterface(service)
        // register callback
        aidlService?.registerListener(aidlCallback)

        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()
    }
}
```

Call AILD methods (Full Example):

<pre class="language-kotlin"><code class="lang-kotlin">// MainActivity.kt
<strong>package com.tejpratapsingh.aildexample
</strong>
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tejpratapsingh.aildexample.databinding.ActivityMainBinding
import com.tejpratapsingh.aildlib.BuildConfig
import com.tejpratapsingh.aildlib.ICalculator
import com.tejpratapsingh.aildlib.ICalculatorCallback
import java.util.Locale

class MainActivity : AppCompatActivity() {

    // callback implemetation
    private val aidlCallback: ICalculatorCallback = object : ICalculatorCallback.Stub() {
        override fun onAdded(result: Int) {
            Toast.makeText(
                applicationContext,
                String.format(Locale.getDefault(), "Sum is: %d", result),
                Toast.LENGTH_LONG
            ).show()
        }
    }
    
    // aild service connection
    private var aidlService: ICalculator? = null
    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            aidlService = ICalculator.Stub.asInterface(service)
            aidlService?.registerListener(aidlCallback)
    
            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()
        }
    }
    
    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val serviceIntent = Intent()
        serviceIntent.component = ComponentName(BuildConfig.AIDL_PACKAGE, BuildConfig.AIDL_SERVICE)

        bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE)

        binding.buttonCallService.setOnClickListener {
            aidlService?.add(2, 2)
        }
    }
}
</code></pre>


---

# 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/aidl-with-callback/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.
