1.死亡回调
java
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
logD("launcher aidl connected");
iLauncher = ILauncher.Stub.asInterface(service);
try {
// 为Binder 对象设置死亡回调,当绑定的服务进程死亡时,会触发这个回调
iLauncher.asBinder().linkToDeath(new IBinder.DeathRecipient() {
// 当绑定的服务进程死亡时,系统会调用这个方法
@Override
public void binderDied() {
Log.d(TAG,"[binderDied] retry bindService!");
// 然后将 iLauncher 置为 null,以释放对旧服务代理对象的引用
iLauncher = null;
// 最后调用 bindService() 方法,尝试重新绑定服务
bindService();
}
},0);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
logD("launcher aidl disconnected");
}
};