android AIDL binderDied

在 Android 的 AIDL (Android Interface Definition Language) 机制中,binderDied 是一个用于处理 Binder 对象死亡的回调。这个机制帮助你在 Binder 进程死掉时进行清理或恢复操作

要实现 binderDied,你需要做以下几步:

  1. 实现 IBinder.DeathRecipient 接口
    你需要创建一个类实现 IBinder.DeathRecipient 接口,以处理 Binder 对象死亡时的事件。
bash 复制代码
    private IBinder.DeathRecipient deathRecipient=new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            Log.i(TAG,"===binderDied===");
            //重新绑定
            bind();
        }
    };
  1. 注册/解绑 DeathRecipient
    在你的代码中,通常是在绑定到服务时,你需要将 DeathRecipient 注册到 Binder 对象,解除对 DeathRecipient 的注册,比如在 onServiceDisconnected 方法中,或者在不再需要时
bash 复制代码
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG,"===onServiceConnected==="+(service != null));
            try {
                service.linkToDeath(deathRecipient,0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG,"===onServiceDisconnected===");
            service.unlinkToDeath(deathRecipient, 0)
        }
    };

在 onServiceConnected 方法中,通过 linkToDeath 注册 DeathRecipient。这样,当 Binder 对象死亡时,binderDied 方法将被调用

相关推荐
怪兽201423 分钟前
请例举 Android 中常用布局类型,并简述其用法以及排版效率
android·面试
应用市场25 分钟前
Android Bootloader启动逻辑深度解析
android
爱吃水蜜桃的奥特曼1 小时前
玩Android Harmony next版,通过项目了解harmony项目快速搭建开发
android·harmonyos
shaominjin1231 小时前
Android 中 RecyclerView 与 ListView 的深度对比:从设计到实践
android
vocal1 小时前
【我的AOSP第一课】AOSP 下载、编译与运行
android
Lei活在当下2 小时前
【业务场景架构实战】8. 订单状态流转在 UI 端的呈现设计
android·设计模式·架构
小趴菜82272 小时前
Android中加载unity aar包实现方案
android·unity·游戏引擎
qq_252924192 小时前
PHP 8.0+ 现代Web开发实战指南 引
android·前端·php
Jeled2 小时前
Android 本地存储方案深度解析:SharedPreferences、DataStore、MMKV 全面对比
android·前端·缓存·kotlin·android studio·android jetpack
2501_915918419 小时前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview