萌妹带你精通aidl设计

发布时间:2026/10/9 4:59:25
萌妹带你精通aidl设计 无aidl怎么实现binder?需求 当触发特殊情况时,使客户端调用服务端 (当某种情况发生时)这个在HomeActivity中模拟 客户端 MyClient.java 手写 bindService() 服务端 MyServer.java 重写 onTransact() 真服务 ClientService 重写 onBind () HomeActivity MyClient myClient new MyClient(); myClient.bindService(this); //延时1秒等待上面异步成功 myClient.callMyServer1();MyClientpublic class MyClient { IBinder mIbinder null; public void bindService(Context context) { Intent intent new Intent(context, ClientService.class); context.bindService(intent, new ServiceConnection() { Override public void onServiceConnected(ComponentName name, IBinder service) { mIbinder service; Log.e(getClass().getName(), zzc addresses service); } Override public void onServiceDisconnected(ComponentName name) { } }, Context.BIND_AUTO_CREATE); } public void callMyServer1() { Parcel data Parcel.obtain(); data.writeInt(1); data.writeInt(2); Parcel replay Parcel.obtain(); try { mIbinder.transact(1001, data, replay, 0); Log.e(getClass().getName(), zzc replay replay.readInt()); } catch (RemoteException e) { throw new RuntimeException(e); } } }MyServerpublic class MyServer extends Binder { Override protected boolean onTransact(int code, NonNull Parcel data, Nullable Parcel reply, int flags) throws RemoteException { if (code 1001) { int a data.readInt(); int b data.readInt(); int sum a b; reply.writeInt(sum); return true; } return super.onTransact(code, data, reply, flags); } }ClientServicepublic class ClientService extends Service { public ClientService() { } Override public IBinder onBind(Intent intent) { return new MyServer(); } }ZHomeActivitypublic class ZHomeActivity extends AppCompatActivity { Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyClient myClient new MyClient(); myClient.bindService(this); Handler handler new Handler(Looper.getMainLooper()); handler.postDelayed(() - { myClient.callMyServer1(); }, 1000); } }总结普通的app服务,不会像AMS这种系统服务注册到ServerManager系统级别的app,引出问题这是基础的模板,但是你可以看到,那个所谓的服务端的add方法,有实无形.导致客户端要调用服务端的add方法,实体得写在客户端//MyClient.java ... public void callMyServer1() { Parcel data Parcel.obtain(); data.writeInt(1); data.writeInt(2); Parcel replay Parcel.obtain(); try { mIbinder.transact(1001, data, replay, 0); Log.e(getClass().getName(), zzc replay replay.readInt()); } catch (RemoteException e) { throw new RuntimeException(e); } }aidl的设计思想是什么呢?

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询