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

相关推荐
Pika1 天前
深入浅出 Compose 测量机制
android·android jetpack·composer
木易 士心1 天前
MPAndroidChart 用法解析和性能优化 - Kotlin & Java 双版本
android·java·kotlin
消失的旧时光-19431 天前
Kotlin Flow 与“天然背压”(完整示例)
android·开发语言·kotlin
ClassOps1 天前
Kotlin invoke 函数调用重载
android·开发语言·kotlin
努力学习的小廉1 天前
初识MYSQL —— 数据类型
android·数据库·mysql
Lei活在当下1 天前
【业务场景架构实战】7. 多代智能手表适配:Android APP 表盘编辑页的功能驱动设计
android·设计模式·架构
手机不死我是天子2 天前
《Android 核心组件深度系列 · 第 2 篇 Service》
android
前行的小黑炭2 天前
Compose页面切换的几种方式:Navigation、NavigationBar+HorizontalPager,会导致LaunchedEffect执行?
android·kotlin·app
前行的小黑炭2 天前
Android :Comnpose各种副作用的使用
android·kotlin·app
BD_Marathon2 天前
【MySQL】函数
android·数据库·mysql