模仿aidl样板代码手写实现aidl。先定义接口 IMiniInterfacepackage com.example.binderdemo; import android.os.IBinder; import android.os.IInterface; import android.os.RemoteException; public interface IMiniInterface extends IInterface { int add(int a, int b) throws RemoteException; // 等价于 AIDL 生成的 DESCRIPTOR String DESCRIPTOR com.example.binderdemo.IMiniInterface; // 等价于 AIDL 生成的 TRANSACTION_xxx int TRANSACTION_add IBinder.FIRST_CALL_TRANSACTION 0; }手写Stubpackage com.example.binderdemo; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; public abstract class MiniStub extends Binder implements IMiniInterface{ public MiniStub() { // 把本地 IInterface 实例和接口唯一标识 DESCRIPTOR绑定到当前 Binder 对象上Binder内部有接口路由表 // 为啥是一个表因为当前MiniStub类可以实现多个接口需要用DESCRIPTOR区分。 // 如果本地调用不跨进程可以queryLocalInterface查询出来。 attachInterface(this, DESCRIPTOR); } Override public IBinder asBinder() { return this; } Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { if (code INTERFACE_TRANSACTION) { // 客户端想查询接口标识 reply.writeString(DESCRIPTOR); return true; } switch (code) { case TRANSACTION_add: { data.enforceInterface(DESCRIPTOR); int a data.readInt(); int b data.readInt(); int result add(a, b); reply.writeNoException(); reply.writeInt(result); return true; } } return super.onTransact(code, data, reply, flags); } }手写Proxypackage com.example.binderdemo; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; public class MiniProxy implements IMiniInterface{ private final IBinder mRemote; // BinderProxy对象 bindService时onServiceConnected中返回的。 public MiniProxy(IBinder remote) { mRemote remote; } Override public IBinder asBinder() { return mRemote; } Override public int add(int a, int b) throws RemoteException { Parcel data Parcel.obtain(); Parcel reply Parcel.obtain(); int result; try { data.writeInterfaceToken(DESCRIPTOR); data.writeInt(a); data.writeInt(b); mRemote.transact(TRANSACTION_add, data, reply, 0); reply.readException(); result reply.readInt(); } finally { reply.recycle(); data.recycle(); } return result; } }修改RemoteService这里我新建一个RemoteService2package com.example.binderdemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; import android.util.Log; import androidx.annotation.Nullable; public class RemoteService2 extends Service { private static final String TAG RemoteService2; private final MiniStub binder new MiniStub() { Override public int add(int a, int b) throws android.os.RemoteException { Log.d(TAG, MiniStub.add( a , b ) thread Thread.currentThread().getName() , pid Process.myPid()); return a b; } }; Nullable Override public IBinder onBind(Intent intent) { Log.d(TAG, onBind thread Thread.currentThread().getName() , pid Process.myPid()); return binder; } }同样指定进程修改MainActivitypackage com.example.binderdemo; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.Process; import android.os.RemoteException; import android.util.Log; import android.widget.TextView; import androidx.activity.ComponentActivity; import androidx.annotation.Nullable; import static com.example.binderdemo.IMyAidlInterface.DESCRIPTOR; public class MainActivity extends ComponentActivity { private static final String TAG ClientMainActivity; // private IMyAidlInterface remote; private IMiniInterface remote; private TextView tv; private boolean bound; private final ServiceConnection conn new ServiceConnection() { Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, onServiceConnected thread Thread.currentThread().getName()); // // 这个service类型是android.os.BinderProxy,远端代理用于跨进程通信。 // // 这里asInterface返回一个客户端Proxy对象里面的成员变量mRemote就是这个service对象。 // remote IMyAidlInterface.Stub.asInterface(service); // 手写版 asInterface因为是跨进程所以这里直接返回 Proxy remote new MiniProxy(service); // android.os.IInterface iin service.queryLocalInterface(DESCRIPTOR); // 跨进程通信时搜索为null // Log.d(TAG, iin iin); // iinnull try { int result remote.add(9527, 0); // String msg remote.getMessage(); // Log.d(TAG, add result result , msg msg); // tv.setText(result result \n msg); tv.setText(result result); Log.d(TAG, 当前客户端进程 Process.myPid()); Log.d(TAG, remote: remote); Log.d(TAG, service: service); } catch (RemoteException e) { e.printStackTrace(); } } Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, onServiceDisconnected); remote null; } }; Override protected void onCreate(Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv new TextView(this); setContentView(tv); // Intent intent new Intent(this, RemoteService.class); Intent intent new Intent(this, RemoteService2.class); bound bindService(intent, conn, BIND_AUTO_CREATE); } Override protected void onDestroy() { if (bound) { unbindService(conn); bound false; } super.onDestroy(); } }运行日志ok.为什么 AIDL 不需要手写 Proxy因为 aidl.exe 编译时根据 .aidl 自动生成了。transact 和 onTransact 的关系客户端调用 transact服务端 Binder 线程回调 onTransact。它们是 Binder IPC 的一对入口。DESCRIPTOR 有什么用data.enforceInterface(DESCRIPTOR) 会校验发来的接口名不匹配就抛异常防止错误调用。TRANSACTION_xxx 有什么用用于在 onTransact 里 switch 分发到具体方法。Parcel 是什么跨进程传输的数据容器序列化参数和返回值。你可以类比为“Binder 专用的 Bundle”。为什么服务端方法不在主线程因为 Binder 驱动为每个进程维护一个 Binder 线程池请求由池中线程处理。
阅读完成 · 觉得有帮助?