🤖
Android Tips
  • 🤖AOSP
    • Clone AOSP repository
    • Directory Structure
    • Setup IDE
      • Setup VSCode
    • Build
      • BluePrint for Application Project
      • BluePrint for Android Library Project
    • Manifest
      • sharedUserId
      • persistant
  • Gradle
    • Create Custom Build Config Variables
    • Create Custom manifest Variables
    • Make app debugable
  • Android Process
    • Find a process by name
    • Kill a process by Id
  • Catch any exception in Android
  • 🎨Theming
    • Theming
      • RRO
        • RRO Internals
        • RRO classes in AOSP
        • RRO Example
        • RRO Permission
      • Fabricated RRO (FRRO)
        • FRRO Example
        • Permission
  • Lifecycle
    • Basics
      • Lifecycle
        • Activity
        • Fragment
          • Fragment add
    • Lifecycle Aware Custom Class
  • â„šī¸Interview
    • Interview Questions
    • Architecture Pattern
      • MVC Architecture pattern
      • MVP Architecture Pattern
      • MVVM Architecture Pattern
  • â†”ī¸AIDL
    • Simple AIDL Communication
      • Creating an AIDL file
      • Create an AIDL server
      • Create AIDL client
      • Limitations
    • AIDL with callback
      • Create AILD interfaces
      • Create an AIDL server
      • Create AIDL client
Powered by GitBook
On this page
  1. AIDL
  2. AIDL with callback

Create AIDL client

// callback implemetation
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()
    }
}
// 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):

// MainActivity.kt
package com.tejpratapsingh.aildexample

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)
        }
    }
}
PreviousCreate an AIDL server

Last updated 1 year ago

â†”ī¸