A应用
(一)新建AIDL文件
interface AidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
String getName();
}
(二)新建服务
class AidlService : Service() {
override fun onBind(intent: Intent?): IBinder? {
return object : AidlInterface.Stub() {
override fun getName(): String {
return "文川雪"
}
}
}
}
(三)注册服务
<service android:name=".AidlService">
<intent-filter>
<action android:name="guozengjie" />
</intent-filter>
</service>
B应用
(一)复制AIDL文件
将A应用的AIDL文件以及所在的文件夹复制到B应用,包名一样
(二)绑定服务
bindService(Intent().apply {
`package` = "club.guozengjie.jetpack"
action = "guozengjie"
}, object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val aidlInterface = AidlInterface.Stub.asInterface(service)
Toast.makeText(this@MainActivity, aidlInterface.name, Toast.LENGTH_LONG).show()
}
}, BIND_AUTO_CREATE)
网友评论