AIDL简单使用

AIDL

跨进程通信(IPC)的方式很多,AIDL只是其中的一种,还有Bundle,文件共享,Messenger,ContentProvider和Socket等进程间通信方式,AIDL是接口定义的语言,具体的通信还要用Binder来进行,Binder是Android独有的跨进程通信,只需要一次拷贝。

  1. 使用的大致流程:
    • 创建.aidl文件,此文件定义带有方法签名的编程接口。
  • 实现接口: AndroidSDK会根据你的.aidl文件,使用java编程语言生成接口,接口拥有一个名为Stub的内部抽象类,用于扩展Binder类并非且实现AIDL接口中的方法,必须扩展Stub类 并实现这些方法。
  • 向客户端提供接口,实现Service并重写onBind() 返回Stub类的实现。
  1. 定义aidl接口
    定义好客户端和服务端的AIDL接口,客户端调用,服务端实现,
interface IPersonManager {
  List<Person> getPersonList();
  //in: 从客户端流向服务端
  boolean addPerson(in Person person);
}

AIDL文件中不是所有的数据类型都支持的

  • Java的基本数据类型(int long char boolen等)
  • String 和 CharSequence
  • List只支持ArrayList
  • Map只支持HashMap
  • Parcelable 所有实现了Parcelable的接口对象
    指示数据走向的方向标记:
  • in:客户端流向服务端
  • out:服务端流向客户端
  • inout:双向流通
    定义一个传输的对象:
class Person(var name: String? = "") : Parcelable {
   constructor(parcel: Parcel) : this(parcel.readString())

   override fun toString(): String {
       return "Person(name=$name) hashcode = ${hashCode()}"
   }

   override fun writeToParcel(parcel: Parcel, flags: Int) {
       parcel.writeString(name)
   }

   fun readFromParcel(parcel: Parcel) {
       this.name = parcel.readString()
   }

   override fun describeContents(): Int {
       return 0
   }

   companion object CREATOR : Parcelable.Creator<Person> {
       override fun createFromParcel(parcel: Parcel): Person {
           return Person(parcel)
       }

       override fun newArray(size: Int): Array<Person?> {
           return arrayOfNulls(size)
       }
   }

新建一个Person.aidl 都完成以后AS会自动成一段代码 IPersonManager.java

package com.xfhy.allinone.ipc.aidl;

public interface IPersonManager extends android.os.IInterface {
    /**
     * Default implementation for IPersonManager.
     */
    public static class Default implements com.xfhy.allinone.ipc.aidl.IPersonManager {
        @Override
        public java.util.List<com.xfhy.allinone.ipc.aidl.Person> getPersonList() throws android.os.RemoteException {
            return null;
        }

        @Override
        public boolean addPerson(com.xfhy.allinone.ipc.aidl.Person person) throws android.os.RemoteException {
            return false;
        }

        @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.xfhy.allinone.ipc.aidl.IPersonManager {
        private static final java.lang.String DESCRIPTOR = "com.xfhy.allinone.ipc.aidl.IPersonManager";

        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * Cast an IBinder object into an com.xfhy.allinone.ipc.aidl.IPersonManager interface,
         * generating a proxy if needed.
         */
        public static com.xfhy.allinone.ipc.aidl.IPersonManager asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.xfhy.allinone.ipc.aidl.IPersonManager))) {
                return ((com.xfhy.allinone.ipc.aidl.IPersonManager) iin);
            }
            return new com.xfhy.allinone.ipc.aidl.IPersonManager.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_getPersonList: {
                    data.enforceInterface(descriptor);
                    java.util.List<com.xfhy.allinone.ipc.aidl.Person> _result = this.getPersonList();
                    reply.writeNoException();
                    reply.writeTypedList(_result);
                    return true;
                }
                case TRANSACTION_addPerson: {
                    data.enforceInterface(descriptor);
                    com.xfhy.allinone.ipc.aidl.Person _arg0;
                    if ((0 != data.readInt())) {
                        _arg0 = com.xfhy.allinone.ipc.aidl.Person.CREATOR.createFromParcel(data);
                    } else {
                        _arg0 = null;
                    }
                    boolean _result = this.addPerson(_arg0);
                    reply.writeNoException();
                    reply.writeInt(((_result) ? (1) : (0)));
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }
        }

        private static class Proxy implements com.xfhy.allinone.ipc.aidl.IPersonManager {
            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;
            }

            @Override
            public java.util.List<com.xfhy.allinone.ipc.aidl.Person> getPersonList() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                java.util.List<com.xfhy.allinone.ipc.aidl.Person> _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    boolean _status = mRemote.transact(Stub.TRANSACTION_getPersonList, _data, _reply, 0);
                    if (!_status && getDefaultImpl() != null) {
                        return getDefaultImpl().getPersonList();
                    }
                    _reply.readException();
                    _result = _reply.createTypedArrayList(com.xfhy.allinone.ipc.aidl.Person.CREATOR);
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            @Override
            public boolean addPerson(com.xfhy.allinone.ipc.aidl.Person person) throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                boolean _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    if ((person != null)) {
                        _data.writeInt(1);
                        person.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    boolean _status = mRemote.transact(Stub.TRANSACTION_addPerson, _data, _reply, 0);
                    if (!_status && getDefaultImpl() != null) {
                        return getDefaultImpl().addPerson(person);
                    }
                    _reply.readException();
                    _result = (0 != _reply.readInt());
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            public static com.xfhy.allinone.ipc.aidl.IPersonManager sDefaultImpl;
        }

        static final int TRANSACTION_getPersonList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_addPerson = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);

        public static boolean setDefaultImpl(com.xfhy.allinone.ipc.aidl.IPersonManager 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.xfhy.allinone.ipc.aidl.IPersonManager getDefaultImpl() {
            return Stub.Proxy.sDefaultImpl;
        }
    }

    public java.util.List<com.xfhy.allinone.ipc.aidl.Person> getPersonList() throws android.os.RemoteException;

    public boolean addPerson(com.xfhy.allinone.ipc.aidl.Person person) throws android.os.RemoteException;
}

IPersonManager是继承了一个android.os.llterface的interface, 然后就是一个Default类,默认实现了IPersonManger,抽象类Stub类继承了android.os.Binder,并且实现了IPersonManager接口,相当于扩展了Binder,因为继承了Binder

  • asInterface 方法用于将服务端的Binder对象转换成客户端所需要的接口对象,如果再同一个进程就返回服务端的Stub对象本身,不是就会返回封装后的Stub.proxy对象。
  • onTransact 是运行再服务端的Binder线程中的,当客户端发起远程请求后,在底层封装好后交给此方法来处理,通过code来区分客户端请求的方法。
  1. 服务端实现接口
    定义一个Service把process设置成一个新的进程,与主进程区分开,实现,aidl成的接口
class RemoteService : Service() {

   private val mPersonList = mutableListOf<Person?>()

   private val mBinder: Binder = object : IPersonManager.Stub() {
       override fun getPersonList(): MutableList<Person?> = mPersonList

       override fun addPerson(person: Person?): Boolean {
           return mPersonList.add(person)
       }
   }

   override fun onBind(intent: Intent?): IBinder? {
       return mBinder
   }

   override fun onCreate() {
       super.onCreate()
       mPersonList.add(Person("Garen"))
       mPersonList.add(Person("Darius"))
   }

}

实现的IPersonManager.Stub是一个Binder 通过onBind()返回,客户端通过这个Binder来跨进程调用Service这边的服务。

image.png

一张图片大体就是这个意思

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容