AIDL : IPC, The Android way
hey, Android help me get data from my other app

Search for a command to run...
hey, Android help me get data from my other app

No comments yet. Be the first to comment.
We all know, each component in Android has its lifecycle, be it Activity, Fragment or Service. Each component gets created and destroys (and a few other stages in between). To create a custom class that is lifecycle aware inherently, we can use a set...

Bit manipulation to set multiple properties to a single field.

Why Powermockito.whenNew() is not enough.

Post inspired by RecyclerCalendarAndroid Lib RecyclerView The theory is simple, we need each day of the calendar rendered as one cell of the RecyclerView. To achieve this, we need to first prepare list of all dates, but there is a problem, we are mi...

The Android Interface Definition Language (AIDL) is similar to other IDLs: it lets you define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
Aidl consists of 3 parts:
AIDL file
Client application
Server application
Enable build feature in your build.gradle.
buildFeatures {
aidl = true
}
Create a new AIDL file using File -> New -> AIDL -> AIDL File
Let's name it ICalculator.aidl
Declare aidl methods:
interface IExampleAidlInterface {
int add(int a, int b);
}
An AIDL server is an app that does the actual work and sends back the result to requesting clients. One service can connect with multiple clients via Binders.
Steps:
Create a service, e.g. CalculatorService extends Service
Create an Implementation of ICalculator.Stub, lets name it CalculatorImpl
Implement methods:
object CalculatorImpl : ICalculator.Stub() {
override fun add(a: Int, b: Int): Int {
return a + b
}
}
Expose your stub implementation with a service binder:
class CalculatorService : Service() {
override fun onBind(intent: Intent): IBinder {
return CalculatorImpl
}
}
Now your service is ready to be consumed by your clients, now let's create a client application.
An AIDL client is an app that binds to an AIDL service, calls API, and shows a response inside its 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()
}
There is one BIG gotcha with AIDL services, which is when the system is in doze mode, your service app might not start to respond to requests.
Here is an example application using AIDL in real life 😎