一、背景
使用AIDL,搭建Client和Server端,实现跨进程通讯,即两个应用之间可以相互通讯。这里列举AIDL实现的方式和需注意的细节,并附上源码。
二、实现方式
2.1 定义AIDL需要的接口,名字为xxx.aidl,Client和Server端 AIDL接口的包名和aidl文件必须一致,在main 目录下,新建aidl目录,如下截图:
Server 端:
Client端:
2.2 IMessageManager.aidl文件
import com.vc.aidlservice.IOnServerCallback;interface IMessageManager {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString);//客户端发送消息给服务端void sendMessageToServer(String message);//服务端发送消息给客户端void sendMessageToClient(String message);//注册监听,用于服务端主动发送消息给客户端void registerListener(IOnServerCallback callback);//注销监听void unRegisterListener();
}
2.3 IOnServerCallback.aidl 文件
package com.vc.aidlservice;// Declare any non-default types here with import statementsinterface IOnServerCallback {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/
// void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
// double aDouble, String aString);//用于客户端接收服务端发送的消息void onMessageReceived(String message);
三、Server端代码
3.1 VcService (AIDL服务端具体逻辑实现)
package com.vc.aidlserver.serviceimport android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlserver.MyApplication
import com.vc.aidlservice.IMessageManager
import com.vc.aidlservice.IOnServerCallback/*** @Time : On 2024/12/6 16:00* @Description : VcService*/
class VcService :Service(){private val TAG="AIDL_Server_"private val SEND_MSG_FAIL=0private val SEND_MSG_SUCCESS=1private var mCallback:IOnServerCallback?=nullprivate val mHanler=object:Handler(Looper.getMainLooper()){override fun handleMessage(msg: Message) {val msgValue=msg.whatif(msgValue==SEND_MSG_FAIL){Toast.makeText(MyApplication.instance,"连接未建立,发送消息失败",Toast.LENGTH_SHORT).show()}else{Toast.makeText(MyApplication.instance,"发送消息成功",Toast.LENGTH_SHORT).show()}}}override fun onBind(intent: Intent): IBinder {return binder}private val binder= object : IMessageManager.Stub() {override fun basicTypes(anInt: Int,aLong: Long,aBoolean: Boolean,aFloat: Float,aDouble: Double,aString: String?) {}/*** 接收客户端发送的消息*/override fun sendMessageToServer(message: String) {Log.d("$TAG", "VcService 服务端收到客户端消息:$message")try {val jsonObject = JSON.parseObject(message)val requestId = jsonObject.getString("requestId")val type = jsonObject.getString("type")val contentObject = jsonObject.getJSONObject("content")val message = contentObject.getString("message")val callbackMessage= MessageInfo.Message("应用:$packageName 收到透传的消息:$message,,,响应返回")val messageInfo=MessageInfo("callback:$requestId", MessageType.Server.toString(),callbackMessage)val jsonMessage= JSONObject.toJSONString(messageInfo)mCallback?.onMessageReceived(jsonMessage)}catch (e:Exception){Log.e("$TAG", "VcService 服务端解析客户端客户端消息异常 Exception:${e.message}")}}/*** 主动发送消息给客户端*/override fun sendMessageToClient(message: String?) {Log.d("$TAG", "VcService 服务端发送消息给客户端:$message,,,mCallback:$mCallback,,,是否主线程:${Looper.getMainLooper()==Looper.myLooper()}")if(mCallback==null){mHanler.sendEmptyMessage(SEND_MSG_FAIL)Log.d("$TAG", "VcService 连接未建立,sendMessageToClient fail,,,message:$message")return}mHanler.sendEmptyMessage(SEND_MSG_SUCCESS)mCallback?.onMessageReceived(message)}/*** 客户端调用注册监听*/override fun registerListener(callback: IOnServerCallback?) {mCallback=callback}/*** 客户端调用注销监听*/override fun unRegisterListener() {Log.d("$TAG", "VcService unRegisterListener")mCallback=null}}}
3.2 Server端主动发送消息给Client端
package com.vc.aidlserverimport android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.RequestIdCreatUtil
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlserver.service.VcService
import com.vc.aidlservice.IMessageManagerclass MainActivity : AppCompatActivity() {private var bt_send: Button? = nullprivate var edt_message: EditText? = nullvar mIMessageManager: IMessageManager? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main_send_message)bt_send = findViewById(R.id.bt_send)edt_message = findViewById(R.id.edt_message)/***在VcService外,获取 mIMessageManager,服务端主动发送消息给AIDL客户端*/val intent = Intent(this, VcService::class.java)val result = bindService(intent, object : ServiceConnection {override fun onServiceConnected(name: ComponentName, service: IBinder) {Log.d("BBBBB", "onServiceConnected 连接服务端成功 name:$name")mIMessageManager = IMessageManager.Stub.asInterface(service) as IMessageManager}override fun onServiceDisconnected(name: ComponentName) {Log.d("BBBBB", "onServiceDisconnected 断开服务端连接 name:$name")}}, BIND_AUTO_CREATE)/*** 服务端主动发送消息给AIDL客户端*/bt_send?.setOnClickListener {Log.d("BBBBB", " mIMessageManager:$mIMessageManager")val message = edt_message?.text.toString().trim()if (message.isNullOrEmpty()) {Toast.makeText(this, "发送消息不能为空", Toast.LENGTH_SHORT).show()return@setOnClickListener}if (mIMessageManager == null) {Toast.makeText(this, "绑定VcService失败,应用发送消息失败", Toast.LENGTH_SHORT).show()} else {val sendMessage = MessageInfo.Message(message)val messageInfo = MessageInfo(RequestIdCreatUtil.creatId(),MessageType.Server.toString(),sendMessage)val jsonMessage = JSONObject.toJSONString(messageInfo)mIMessageManager!!.sendMessageToClient(jsonMessage)//Toast.makeText(this,"应用发送消息成功",Toast.LENGTH_SHORT).show()Log.d("BBBBB", "sendMessageToClient:$jsonMessage")}}}}
3.3 AndroidManifest.xml配置
<service android:name=".service.VcService"android:enabled="true"android:exported="true"android:process=":remote"><intent-filter><action android:name="com.vc.aidlserver.service.VcService" /></intent-filter></service>
四、Client端代码
4.1 Client 逻辑代码
package com.vc.aidlclientimport android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlservice.IMessageManager
import com.vc.aidlservice.IOnServerCallbackprivate var mIMessageManager: IMessageManager? = null
private var isConnect = falseclass MainActivity : AppCompatActivity(), View.OnClickListener {private var bt_connect: Button? = nullprivate var bt_send: Button? = nullprivate var bt_disconnect: Button? = nullprivate var isBound = falseprivate val TAG = "AIDL_Client_"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)bt_connect = findViewById(R.id.bt_connect)bt_send = findViewById(R.id.bt_send)bt_disconnect = findViewById(R.id.bt_disconnect)bt_connect?.setOnClickListener(this)bt_send?.setOnClickListener(this)bt_disconnect?.setOnClickListener(this)}/*** 连接服务端*/fun connectServer() {val intent = Intent()intent.setComponent(ComponentName("com.vc.aidlserver","com.vc.aidlserver.VcService"))isBound = bindService(intent, mServiceConnection, BIND_AUTO_CREATE)Log.d("$TAG", "bindService isBound:$isBound")}private val serverListener = object : IOnServerCallback.Stub() {override fun onMessageReceived(message: String?) {Log.d("$TAG", "onMessageReceived :$message")}}/*** 销毁监听器*/private fun destroyService() {try {if (isBound) {isConnect = falsemIMessageManager?.unRegisterListener()mIMessageManager = nullunbindService(mServiceConnection)}} catch (e: Exception) {e.printStackTrace()Log.d("$TAG", "unbindService Exception:${e.message}")}}override fun onDestroy() {Log.d("$TAG", "onDestroy()")super.onDestroy()destroyService()}override fun onClick(v: View) {if (v.id == R.id.bt_connect) {if (isConnect) {Toast.makeText(this, "已连接服务端", Toast.LENGTH_SHORT).show()} else {connectServer()}} else if (v.id == R.id.bt_send) {if (mIMessageManager == null || !isConnect) {Toast.makeText(this, "连接未建立", Toast.LENGTH_SHORT).show()} else {val message = MessageInfo.Message("我是客户端")val messageInfo = MessageInfo(RequestIdCreatUtil.creatId(),MessageType.Client.toString(),message)val jsonMessage = JSONObject.toJSONString(messageInfo)Log.d("$TAG", "客户端发送消息给服务端 jsonMessage->>>:$jsonMessage")mIMessageManager?.sendMessageToServer(jsonMessage)}} else if (v.id == R.id.bt_disconnect) {destroyService()}}private val mServiceConnection = object : ServiceConnection {override fun onServiceConnected(name: ComponentName, service: IBinder) {// 服务连接成功,你可以开始与服务交互Log.d("$TAG", "onServiceConnected 连接服务端成功 name:$name")isConnect = truemIMessageManager = IMessageManager.Stub.asInterface(service) as IMessageManagermIMessageManager?.registerListener(serverListener)}override fun onServiceDisconnected(name: ComponentName) {// 服务断开连接Log.d("$TAG", "onServiceDisconnected 断开服务端连接 name:$name")destroyService()}}
}
4.2 build.gradle配置
注:不配置无法生效aidl文件,Server端也一样,需要配置,有可能和AS版本有关,博主Android studio版本:Android Studio Koala Feature Drop | 2024.1.2
4.3 AndroidManifest.xml
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"tools:ignore="QueryAllPackagesPermission"/><queries><package android:name="com.vc.aidlserver"/></queries>
Client需要配置以上,否则存在连接不上服务端现象
五、完整源码下载
源码下载连接:https://download.csdn.net/download/banzhuantuqiang/90159482