Android android.os.DeadObjectException aidl通信异常分析及解决

问题描述

做一款音乐播放应用,播放服务是通过AIDL形式对外暴露,允许跨进程调用且多个App同时操作音乐播放,偶现android.os.DeadObjectException问题

复制代码
12-15 09:28:12.371: W/System.err(5412): android.os.DeadObjectException
12-15 09:28:12.372: W/System.err(5412): 	at android.os.BinderProxy.transactNative(Native Method)
12-15 09:28:12.372: W/System.err(5412): 	at android.os.BinderProxy.transact(Binder.java:496)

问题分析

在使用aidl进行进程间通信时,有时候在客户端调用服务端的接口会抛出DeadObjectException异常,原因一般是由于某种原因服务端程序崩溃重启或者服务对象由于内存紧张被回收导致的

问题解决

方案1

针对应用开发,可以在服务端进程启动的时候发个消息给客户端,客户端收到消息的时候重新进行绑定操作,目的是为了同步客户端和服务端的连接,客户端进程启动的时候也要绑定一次(注:在已经连接的情况下,服务端由于某种原因进程重启了,如果客户端没有收到回调,客户端保存的连接不为空,这时调用服务端接口就会抛出DeadObjectException异常)

方案2

调用的对象不存在,因为它所在App进程不存在或进程崩溃,此时在底层回调时报错。

那么在调用对象之前,建议检查是否存在此进程。DeadObjectException异常出现是因为App进程不存在或进程崩溃因此在底层回调时报错。进程不存在或者进程崩溃需要查看其他日志来定位。对于进程崩溃的情况,也有可能部分原因是由于操作系统考虑到内存、CPU、优先级等指标,选择杀死一个进程得到资源。

java 复制代码
  ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pidsTask = activityManager.getRunningAppProcesses();
    for(int i = 0; i < pidsTask.size(); i++) {
        nameList.add(pidsTask.get(i).processName);
    idList.add(pidsTask.get(i).uid);
    }
    If(nameList.contains("processName")){
       // 进程存在
    }
    else{
       // 进程不存在
    }

方案2

进行死亡监听

1)在调用服务端接口的时候先进行判断bind是否还活着

java 复制代码
if (mIMyAidlInterface != null && mIMyAidlInterface.asBinder().isBinderAlive()) {
    try {
        mIMyAidlInterface.startRecord();
    } catch (Exception e) {
        Log.e(TAG, "Exception");
        e.printStackTrace();
    }
}

2)注册死亡代理

java 复制代码
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {

    @Override
    public void binderDied() {                           
    // 当绑定的service异常断开连接后,自动执行此方法
        Log.e(TAG,"binderDied " );
        if (mIMyAidlInterface != null){
    // 当前绑定由于异常断开时,将当前死亡代理进行解绑        mIMyAidlInterface.asBinder().unlinkToDeath(mDeathRecipient, 0);
            //  重新绑定服务端的service
            bindService(new Intent("com.service.bind"),mMyServiceConnection,BIND_AUTO_CREATE);      
        }
    }
};

3)在service绑定成功后,调用linkToDeath()注册进service,当service发生异常断开连接后会自动调用binderDied()

java 复制代码
public void onServiceConnected(ComponentName name, IBinder service) {          
    //绑定成功回调
    Log.d(TAG, "onServiceConnected");
    mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);     
    //获取服务端提供的接口
    try {
    // 注册死亡代理
    if(mIMyAidlInterface != null){
    Log.d(TAG, mIMyAidlInterface.getName());
    service.linkToDeath(mDeathRecipient, 0); 
    }       
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
相关推荐
私人珍藏库1 小时前
[Android] 一木百宝箱-万能百宝箱+抖音去水印等几百种功能
android·人工智能·app·软件·多功能
wupa2 小时前
在 enableEdgeToEdge 模式下处理软键盘与自定义面板协同
android
plainGeekDev3 小时前
ProGuard → R8
android·java·kotlin
雨白4 小时前
C 语言字符串:从字符数组、指针到核心操作实战
android
Java小白笔记5 小时前
MySQL中存储过程大表分批删除历史数据
android·mysql·adb
aidou13145 小时前
Kotlin中沉浸式状态栏显示布局
android·kotlin·windowmanager·沉浸式状态栏·insetslistener·insetscompat·systembars
我命由我123456 小时前
Android 构建项目问题:XML 片段出错,com.ctc.wstx.exc.WstxUnexpectedCharException
android·xml·android studio·android jetpack·android-studio·android runtime
阿pin6 小时前
Android随笔-pipe是什么?
android·linux·pipe
帅次6 小时前
Android 高级工程师面试:Flow 与状态流 近1年高频追问 22 题
android·面试·职场和发展
雨季余静6 小时前
RustDesk Android APK 从零编译全步骤
android