注意:以下代码,与前面的文章息息相关,这里只介绍关键代码,其余代码不展示
核心概念
当客户端拿到一个 HAL 的 AIDL 接口代理对象后,可以对这个远端 Binder 注册一个 death recipient:
- 如果 HAL 进程挂了
- 或者承载该 Binder 的进程退出了
- Binder driver 会通知客户端
- 客户端注册的回调会被触发
这就是 linkToDeath。
涉及到的关键方法与类
路径: frameworks/native/libs/binder/ndk/include_ndk/android/binder_ibinder.h
/**
* 表示对死亡通知的处理句柄。参见 AIBinder_linkToDeath/AIBinder_unlinkToDeath。
*/
struct AIBinder_DeathRecipient;
typedef struct AIBinder_DeathRecipient AIBinder_DeathRecipient;
/**
* 此函数在接收到死亡通知时执行。参见 AIBinder_linkToDeath/AIBinder_unlinkToDeath。
*
* 自 API 级别 29 起可用。
*
* \param cookie 传递给 AIBinder_linkToDeath 的 cookie。
*/
typedef void (*AIBinder_DeathRecipient_onBinderDied)(void* cookie) __INTRODUCED_IN(29);
/**
* 创建一个新的binder death recipient。该对象可以附加到多个不同的binder对象。
*
* 自 API 级别 29 起可用。
*
* 警告:请确保此 Cookie 的生命周期足够长。如果它是动态分配的,
* 则应使用 `AIBinder_DeathRecipient_setOnUnlinked` 将其删除。
* onBinderDied:调用此死亡接收者时要执行的回调函数。
* return`:新创建的对象(如果 `onBinderDied` 为 null,则返回 null)。
*/
__attribute__((warn_unused_result)) AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
AIBinder_DeathRecipient_onBinderDied onBinderDied) __INTRODUCED_IN(29);
/**
* 注册以接收关联 Binder 已终止的通知。同一个终止接收者可以关联多个不同的 Binder。
* 如果 Binder 是本地 Binder,则不会指定终止接收者(因为如果本地进程终止,
* 则不存在接收者来接收事务)。如果 Binder 终止,则会将 cookie 传递给接收者,
* 该 cookie 可以为空。必须使用此 coo


525

被折叠的 条评论
为什么被折叠?



