Create AIDL client
An AIDL client connects to a exposed AIDL Service via Binders
An AIDL client is an app that binds to an AIDL service, calls API, shows response inside its own views.
Steps:
Create a service connection:
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() } }
Connect to AIDL service via Binders.
val serviceIntent = Intent() serviceIntent.component = ComponentName(BuildConfig.AIDL_PACKAGE, BuildConfig.AIDL_SERVICE) bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE)
Call methods of AIDL after successful connection:
aidlService?.add(2, 2)?.let { Toast.makeText( applicationContext, String.format(Locale.getDefault(), "Sum is: %d", it), Toast.LENGTH_LONG ).show() }
Last updated