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 方法将被调用

相关推荐
走在路上的菜鸟18 分钟前
Android学Dart学习笔记第二十三节 类-扩展类型
android·笔记·学习·flutter
百***78751 小时前
【技术教程】3步极速接入GPT-5.1:零门槛体验多模态AI能力
android·java·人工智能·gpt·opencv
走在路上的菜鸟1 小时前
Android学Dart学习笔记第二十一节 类-点的简写
android·笔记·学习·flutter
QQ12958455042 小时前
ThingsBoard-修改Android APP应用程序名和描述
android·物联网·iot
代码代码快快显灵2 小时前
Android项目架构深度解析
android
丐中丐9992 小时前
一个Binder通信中的多线程同步问题
android
诸神黄昏EX2 小时前
Android Qualcomm USB 专题系列【篇二:UsbGadget模式配置】
android
vocal2 小时前
【我的AOSP第一课】Android Init 语言与 rc 文件
android
诸神黄昏EX3 小时前
Android Qualcomm USB 专题系列【总篇:USB HAL架构】
android·linux·网络
原神启动13 小时前
Ansible(三)—— 使用Ansible自动化部署LNMP环境
android·自动化·ansible