一、服务端死亡回调在绑定服务端时可以获取服务端时的 Binder 对象调用 Binder 对象的 linkToDeath 方法注册死亡回调监听服务端时是否挂了privateIMyAidlInterfacemyAidlInterface;privatebooleanisBoundfalse;privatefinalIBinder.DeathRecipientdeathRecipientnewIBinder.DeathRecipient(){OverridepublicvoidbinderDied(){Log.i(TAG,检测到服务端进程死亡);}};privateServiceConnectionconnectionnewServiceConnection(){OverridepublicvoidonServiceConnected(ComponentNamename,IBinderservice){Log.i(TAG,服务连接成功);myAidlInterfaceIMyAidlInterface.Stub.asInterface(service);try{service.linkToDeath(deathRecipient,0);}catch(RemoteExceptione){e.printStackTrace();}isBoundtrue;}OverridepublicvoidonServiceDisconnected(ComponentNamename){Log.i(TAG,服务连接断开);myAidlInterfacenull;isBoundfalse;// 系统会处理 unlinkToDeath、unbindService}};触发 onServiceDisconnected 回调系统会处理 unlinkToDeath、unbindService不需要手动调用详见源码// 1. doDeath 方法源码publicvoiddoDeath(ComponentNamename,IBinderservice){synchronized(this){ConnectionInfooldmActiveConnections.get(name);if(oldnull||old.binder!service){// Death for someone different than who we last// reported... just ignore it.return;}mActiveConnections.remove(name);old.binder.unlinkToDeath(old.deathMonitor,0);}mConnection.onServiceDisconnected(name);}// doDeath 方法中的清理逻辑mActiveConnections.remove(name);old.binder.unlinkToDeath(old.deathMonitor,0);// 2. doConnected 方法publicvoiddoConnected(ComponentNamename,IBinderservice,booleandead){ServiceDispatcher.ConnectionInfoold;ServiceDispatcher.ConnectionInfoinfo;synchronized(this){if(mForgotten){// We unbound before receiving the connection; ignore// any connection received.return;}oldmActiveConnections.get(name);if(old!nullold.binderservice){// Huh, already have this one. Oh well!return;}if(service!null){// A new service is being connected... set it all up.infonewConnectionInfo();info.binderservice;info.deathMonitornewDeathMonitor(name,service);try{service.linkToDeath(info.deathMonitor,0);mActiveConnections.put(name,info);}catch(RemoteExceptione){// This service was dead before we got it... just// dont do anything with it.mActiveConnections.remove(name);return;}}else{// The named service is being disconnected... clean up.mActiveConnections.remove(name);}if(old!null){old.binder.unlinkToDeath(old.deathMonitor,0);}}// If there was an old service, it is now disconnected.if(old!null){mConnection.onServiceDisconnected(name);}if(dead){mConnection.onBindingDied(name);}else{// If there is a new viable service, it is now connected.if(service!null){mConnection.onServiceConnected(name,service);}else{// The binding machinery worked, but the remote returned null from onBind().mConnection.onNullBinding(name);}}}// doConnected 方法中的清理逻辑if(old!null){old.binder.unlinkToDeath(old.deathMonitor,0);}二、服务端与客户端的线程环境1、服务端AIDL 接口在主线程执行无论客户端是在主线程调用还是在子线程调用privatefinalIMyAidlInterface.StubbindernewIMyAidlInterface.Stub(){Overridepublicintadd(inta,intb)throwsRemoteException{// Binder 线程returnab;}}DeathRecipient 回调在 Binder 线程执行IBinder.DeathRecipientdrnewDeathRecipient(){OverridepublicvoidbinderDied(){// Binder 线程}};2、客户端ServiceConnection 回调在主线程执行privateServiceConnectionconnectionnewServiceConnection(){OverridepublicvoidonServiceConnected(ComponentNamename,IBinderservice){// 主线程}OverridepublicvoidonServiceDisconnected(ComponentNamename){// 主线程}};DeathRecipient 回调在 Binder 线程执行privatefinalIBinder.DeathRecipientdeathRecipientnewIBinder.DeathRecipient(){OverridepublicvoidbinderDied(){// 主线程}};AIDL 接口调用调用前后都是在调用者线程执行try{// 主线程intresultmyAidlInterface.add(5,3);Log.i(TAG,add result: result);// 主线程}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,add method error: e.getMessage());}newThread(()-{try{// 子线程intresultmyAidlInterface.add(5,3);Log.i(TAG,add result: result);// 子线程}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,add method error: e.getMessage());}}).start();AIDL 接口回调在主线程执行无论客户端是在主线程注册还是在子线程注册callbacknewIPlayerCallback.Stub(){OverridepublicvoidonSongChanged(StringsongName)throwsRemoteException{// 主线程Log.i(TAG,onSongChanged: songName);}OverridepublicvoidonPlayStateChanged(booleanisPlaying)throwsRemoteException{// 主线程Log.i(TAG,onPlayStateChanged: isPlaying);}};try{myAidlInterface.registerCallback(callback);Log.i(TAG,registerCallback method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,registerCallback method error: e.getMessage());}newThread(()-{callbacknewIPlayerCallback.Stub(){OverridepublicvoidonSongChanged(StringsongName)throwsRemoteException{// 主线程Log.i(TAG,onSongChanged: songName);}OverridepublicvoidonPlayStateChanged(booleanisPlaying)throwsRemoteException{// 主线程Log.i(TAG,onPlayStateChanged: isPlaying);}};try{myAidlInterface.registerCallback(callback);Log.i(TAG,registerCallback method success);}catch(RemoteExceptione){e.printStackTrace();Log.e(TAG,registerCallback method error: e.getMessage());}}).start();三、oneway 关键字1、基本介绍oneway 关键字修饰的方法客户端调用后立即返回不等待服务端执行完成onewayvoiddoSomething();2、注意事项oneway 方法不能有返回值否则编译报错onewayStringdoSomething();# 输出结果 oneway method doSomething cannot return a valueoneway 方法的参数可以用 in 修饰onewayvoiddoSomething(inbyte[]data);oneway 方法的参数不能用 out 或 inout 修饰否则编译报错onewayvoiddoSomething(outbyte[]data);# 输出结果 oneway method doSomething cannot have out parametersonewayvoiddoSomething(inoutbyte[]data);# 输出结果 oneway method doSomething cannot have out parameters