AIDL是Android Interface Definition Language的缩写,即为Android接口定义语言。安卓系统中会有多个进程,每个进程都是相互独立的,无法共享内存。想要共享进程间的数据,就需要进程通信,AIDL即为IPC机制(跨进程通信)的一种。
FW向app层提供的接口大多使用的就是AIDL(还有使用广播的,例如Home按键、蓝牙连接,USB接续等等),封装在了framework.jar中了。
接下来进入主题,看看DIAL怎么使用。
首先在main的路径下新建一个文件夹为aidl。
在aidl文件夹中新建一个aidl文件IEx.aidl
在IEx.aidl文件中写出客户端需要使用的接口
package com.android.ex;
interface IEx {
/**
* control ex
*/
oneway void controlEx();
}
以上写完之后对项目进行build,build完成之后会生成对应的IEx.java文件
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.android.ex;
public interface IEx extends android.os.IInterface
{
/** Default implementation for IEx. */
public static class Default implements com.android.ex.IEx
{
/**
* control ex
*/
@Override public void controlEx() throws android.os.RemoteException
{
}
@Override
public android.os.IBinder asBinder() {
return null;
}
}
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.android.ex.IEx
{
private static final java.lang.String DESCRIPTOR = "com.android.ex.IEx";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.android.ex.IEx interface,
* generating a proxy if needed.
*/
public static com.android.ex.IEx asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.android.ex.IEx))) {
return ((com.android.ex.IEx)iin);
}
return new com.android.ex.IEx.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
java.lang.String descriptor = DESCRIPTOR;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
case TRANSACTION_controlEx:
{
data.enforceInterface(descriptor);
this.controlEx();
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.android.ex.IEx
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
* control ex
*/
@Override public void controlEx() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_controlEx, _data, null, android.os.IBinder.FLAG_ONEWAY);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().controlEx();
return;
}
}
finally {
_data.recycle();
}
}
public static com.android.ex.IEx sDefaultImpl;
}
static final int TRANSACTION_controlEx = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
public static boolean setDefaultImpl(com.android.ex.IEx impl) {
// Only one user of this interface can use this function
// at a time. This is a heuristic to detect if two different
// users in the same process use this function.
if (Stub.Proxy.sDefaultImpl != null) {
throw new IllegalStateException("setDefaultImpl() called twice");
}
if (impl != null) {
Stub.Proxy.sDefaultImpl = impl;
return true;
}
return false;
}
public static com.android.ex.IEx getDefaultImpl() {
return Stub.Proxy.sDefaultImpl;
}
}
/**
* control ex
*/
public void controlEx() throws android.os.RemoteException;
}
接下来是我们的服务端代码,AIDL的实现使用的binder机制,所以服务端需要再Service的onBind方法中返回Stub的实例。
import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.android.ex.IEx
class ExService : Service() {
override fun onBind(intent: Intent): IBinder {
return ExImpl()
}
override fun onCreate() {
super.onCreate()
}
inner class ExImpl : IEx.Stub() {
override fun controlEx() {
TODO("Not yet implemented")
}
}
}
客户端实现代码如下:
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import com.android.ex.IEx
/**
* @ClassName Ex
* @Author LZD
* @Date 2024/1/24 11:20
* @Description
*/
class Ex : IEx {
companion object {
private var sInstance: Ex? = null
}
private var mService: IEx? = null
fun getInstance(): Ex? {
if (null == sInstance) {
synchronized(Ex::class) {
if (null == sInstance) {
sInstance = Ex()
}
}
}
return sInstance
}
private val connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
mService = IEx.Stub.asInterface(service)
try {
mService?.asBinder()?.linkToDeath({
mService = null
getService()
}, 0);
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onServiceDisconnected(name: ComponentName?) {
mService = null
}
}
private lateinit var mContext: Context
public fun bind(context: Context) {
mContext = context
getService()
}
public fun unBind() {
if (null != mContext) {
mContext.unbindService(connection);
}
}
@Synchronized
private fun getService(): IEx? {
if (null == mService) {
var intent = Intent();
val cn = ComponentName("com.android.ex", "com.android.ex.ExService")
intent.setComponent(cn)
mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
return mService
}
override fun asBinder(): IBinder = mService!!.asBinder()
override fun controlEx() {
if (mService != null) {
try {
mService?.controlEx()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
以上为AIDL的简单使用方法。