This is a reactive implementation of AIDL with examples of how to use the lib
AIDL is Android Interface Definition Language, a component of the Android framework that allows to separate apps (processes) to communicate with each other using a "contract" (interface).
To observe this example project work, you must first install the receiver
module on your device
(it doesn't show any UI, it's just a Service
). Once it's installed, install and run the app
module
on your device. The app
module will display UI that starts the service, binds with the service,
and uses methods declared in the service.
Note it also tells you how long it took to receive the entire response from the Service, AIDL is very fast compared to other forms of IPC.
Add the JitPack
repository to the root build.gradle
file of your project
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Import the library in your app's module build.gradle
dependencies {
...
implementation 'com.github.xiaogegexiao:rx-aidl:1.2'
}
##AIDL
// IRandomNumberService.aidl
package com.xiao.aidlexamplereceiver;
// Declare any non-default types here with import statements
interface IRandomNumberService {
int getRandomNumber(int range);
}
To bind to a service thro AIDL
private var mDisposable: Disposable? = null
val serviceIntent = Intent().setComponent(ComponentName(
"com.xiao.aidlexamplereceiver",
"com.xiao.aidlexamplereceiver.RandomNumberService"))
mDisposable?.dispose()
mDisposable = RxAIDLObservable<IRandomNumberService, IRandomNumberService.Stub>(this, serviceIntent, IRandomNumberService::class.java, IRandomNumberService.Stub::class.java)
.compose(bindUntilEvent(ActivityEvent.PAUSE))
.subscribe({
log.append("onNext.\n")
mRandomNumberService = it
getRandomNumber()
}, {
log.append("onError ${it.message}.\n")
}, {
log.append("onComplete.\n")
})
To unbind to a service
mDisposable?.dispose()