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

相关推荐
simplepeng5 小时前
我的天,我真是和androidx的字体加载杠上了
android
小猫猫猫◍˃ᵕ˂◍6 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
CYRUS_STUDIO8 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
梦否8 小时前
【Android】类加载器&热修复-随记
android
徒步青云8 小时前
Java内存模型
android
今阳9 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos
-优势在我13 小时前
Android TabLayout 实现随意控制item之间的间距
android·java·ui
hedalei13 小时前
android13修改系统Launcher不跟随重力感应旋转
android·launcher
Indoraptor15 小时前
Android Fence 同步框架
android
峥嵘life15 小时前
DeepSeek本地搭建 和 Android
android