恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

手写迷你aidl

  • 首页
  • 资讯中心
  • /
  • 手写迷你aidl

相关资讯

数信杯一血JIT 2026/10/18 7:26:35
Webpack迁到Vite第七节(四):全家桶移除 — 保留项、脚本与数据收尾 2026/10/18 7:26:35
三层交换机、LACP与路由器混合组网实战指南 2026/10/18 7:21:35

最新资讯

黄河流域GIS空间数据包:生产级质检与ArcGIS水文分析实战
BakeMesh粒子层级错乱怎么办?三种方案解决网格飞回原点
21.5寸嵌入式工控一体机:真正硬核的产线终端控制方案全解析
捷远BOM管理系统单机版:免安装数据库的物料清单工具实战指南
整站下载器实战:从wget到Python增量更新与避坑指南
AngularJS Scope机制深度解析:从原型继承到脏检查

今日推荐

基于GMM的语音识别系统原理与MATLAB实现详解
数字图像处理入门:反白、灰度化、调色板修改与导出txt实践
机器人大脑技术盘点:具身智能的感知、决策与数据闭环

本周热门

基于GMM的语音识别系统原理与MATLAB实现详解
数字图像处理入门:反白、灰度化、调色板修改与导出txt实践
机器人大脑技术盘点:具身智能的感知、决策与数据闭环

本月精选

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证
2026 大模型集体涨价:用 Python 做企业 Token 成本测算与选型避坑(附配置)

手写迷你aidl

发布时间:2026/10/18 7:26:35
手写迷你aidl 模仿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 线程池请求由池中线程处理。

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号