一、 名词解释
AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似。 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口。 在 Android 上,一个进程通常无法访问另一个进程的内存。 尽管如此,进程需要将其对象分解成操作系统能够识别的原语,并将对象编组成跨越边界的对象。 编写执行这一编组操作的代码是一项繁琐的工作,因此 Android 会使用 AIDL 来处理。Android developers
二、使用场景(本文只描述单个app内)
- activity to server
- server to activity
三、 使用方法
- 创建文件IMyServerAidlInterface
package com.ly.testaidl;
// Declare any non-default types here with import statements
import com.ly.testaidl.ActivityAidlCallback;
interface IMyServerAidlInterface {
void registerReceiver(ActivityAidlCallback callback);
void push(String data);
}
- 创建回调文件(server to activity)
package com.ly.testaidl;
// Declare any non-default types here with import statements
import android.os.Bundle;
interface ActivityAidlCallback {
void push(String action,in Bundle data);
}
- 服务端(server)
class MyService : Service() {
private var callback: ActivityAidlCallback? = null
private val server = object : IMyServerAidlInterface.Stub() {
override fun push(data: String?) {
println("Server receive data$data this time is ->${System.currentTimeMillis()}")
}
override fun registerReceiver(callback: ActivityAidlCallback?) {
this@MyService.callback = callback
}
}
override fun onBind(intent: Intent): IBinder {
return server
}
override fun onCreate() {
super.onCreate()
Thread {
while (true) {
if (callback != null) {
callback!!.push("test${System.currentTimeMillis()}", null)
}
Thread.sleep(2000)
}
}.start()
}
}
说明时间:
object : IMyServerAidlInterface.Stub()
通过创建这个对象 得到IMyServerAidlInterface 对象之后 再通过onBind()
的时候讲此对象传给绑定对象。这样 bindServer的activity就有了一个
IMyServerAidlInterface
对象,也就可以调用其方法了
- 客户端(activity)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent()
intent.setClass(this, MyService::class.java)
bindService(intent, object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val binder = IMyServerAidlInterface.Stub.asInterface(service)
binder.registerReceiver(object : ActivityAidlCallback.Stub() {
override fun push(action: String?, data: Bundle?) {
println("收到数据$action this time is ->${System.currentTimeMillis()}")
binder.push("activity push ${System.currentTimeMillis()} ")
}
})
}
}, Context.BIND_AUTO_CREATE)
}
}
说明时间:
bindServer之后 可以在
onServiceConnected()
方法里获得一个IBinder
对象通过
IMyServerAidlInterface.Stub.asInterface(service)
就可以拿到Server那边传过来的接口对象了拿到
IMyServerAidlInterface
之后 就可以调用其方法 跟Server通讯啦
但是! 只跟Server主动通讯是不够的 业务逻辑里 Server完成了某个耗时的事务之后 还需要通知我们activity
使用Interface 进行反向通讯
拿到IMyServerAidlInterface
之后 调用IMyServerAidlInterface.registerReceiver(ActivityAidlCallback callback)
传递一个callback到Server端 这样 server端收到这个callback之后就可以通过调用 ActivityAidlCallback.push()
来跟activity通讯
这样 我们就有了一个双向通讯的方法啦! (广播什么的 实在是太麻烦了)