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

相关推荐
_extraordinary_1 小时前
MySQL 事务(二)
android·数据库·mysql
鸿蒙布道师5 小时前
鸿蒙NEXT开发动画案例5
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei
橙子1991101610 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork11 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin
笨鸭先游11 小时前
Android Studio的jks文件
android·ide·android studio
gys989512 小时前
android studio开发aar插件,并用uniapp开发APP使用这个aar
android·uni-app·android studio
H3091912 小时前
vue3+dhtmlx-gantt实现甘特图展示
android·javascript·甘特图
像风一样自由12 小时前
【001】renPy android端启动流程分析
android·gitee
千里马学框架13 小时前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
xianrenli3819 小时前
android特许权限调试
android