相关文章推荐

一、异常原因

在使用aidl进行进程间通信时,有时候在客户端调用服务端的接口会抛出DeadObjectException异常,原因一般是由于某种原因服务端程序崩溃重启或者服务对象由于内存紧张被回收导致的,最近开发的时候遇到过此问题,解决方案有两种,实测有效。

二、解决方案如下两种方案

1. 方案一:针对应用开发,可以在服务端进程启动的时候发个消息给客户端,客户端收到消息的时候重新进行绑定操作,目的是为了同步客户端和服务端的连接,客户端进程启动的时候也要绑定一次(注:在已经连接的情况下,服务端由于某种原因进程重启了,如果客户端没有收到回调,客户端保存的连接不为空,这时调用服务端接口就会抛出DeadObjectException异常)
2. 方案二:进行死亡监听
1)在调用服务端接口的时候先进行判断bind是否还活着
if (mIMyAidlInterface != null && mIMyAidlInterface.asBinder().isBinderAlive()) {
    try {
        mIMyAidlInterface.startRecord();
    } catch (Exception e) {
        Log.e(TAG, "Exception");
        e.printStackTrace();
2)注册死亡代理
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()
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();
                    一、异常原因在使用aidl进行进程间通信时,有时候在客户端调用服务端的接口会抛出DeadObjectException异常,原因一般是由于某种原因服务端程序崩溃重启或者服务对象由于内存紧张被回收导致的,最近开发的时候遇到过此问题,解决方案有两种,实测有效。二、解决方案如下两种方案1. 方案一:针对应用开发,可以在服务端进程启动的时候发个消息给客户端,客户端收到消息的时候重新进行绑定操作,目的...
andorid.os.DeadObjectException
android.os.DeadObjectException 07-13 18:28:45.398: W/System.err(32272): at android.os.BinderProxy.transact(Native Method)
我们项目有2个进程
				
开发的过程中有时候会遇到DeadObjectException,说明系统service已经停止运行,解决的方式是在mainfistxml的application标签中添加android:hardwareAccelerated="false",禁用硬件加速。 转载于:https://www.cnblogs.com/henkun010/p/6566015.html...
碰到一个异常,具有随机性:     android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died         at android.os.BinderProxy.transactNative(Native Method)         at android.o...
出现异常如下: System.err: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died 10-30 09:51:08.151  3664  3682 W System.err:     at android.os.BinderProxy.transac...
今天出现了android.os.DeadObjectException异常。那么DeadObjectException什么意思呢,字面意思当前对象“死”了,也就是没有了呗!那好首先来看看完全的log日志: 01-12 14:55:45.934 1240-1296/? W/WindowAnimator: Failed to dispatch window animation state change.
事情的起因是解决如何判断aidl服务端使用的客户端对象是否为同一个对象。于是引发了asBinder的使用,RemoteCallbackList使用,以及很少使用的IBinder.DeathRecipient。 此篇介绍可以帮助解决aidl使用中的生命周期控制、对象维护以及资源释放,保活服务问题。 当服务端接口有接收客户端aidl定制的对象时,例如维护一个listener列表。你可能需要避免相同对象被重复添加到集合。这个时候需要在服务端接口内对对象执行asBinder操作,此操作返回
 
推荐文章