Android IBinder的linkToDeath介绍

先看官方解释:

 

就是说我们通过systemService申请了某个service的Binder后,可以调用这个IBinder的linkToDeath函数进行注册。可以注册一个IBinder.DeathRecipient类型的对象。其中IBinder.DeathRecipient是IBinder类中定义的一个嵌入类。
public abstract void linkToDeath (IBinder.DeathRecipient recipient, int flags)
Since: API Level 1

Register the recipient for a notification if this binder goes away. If this binder object unexpectedly goes away (typically because its hosting process has been killed), then the given IBinder.DeathRecipient's DeathRecipient.binderDied() method will be called.

You will only receive death notifications for remote binders, as local binders by definition can't die without you dying as well.

当这个IBinder所对应的Service进程被异常的退出时,比如被kill掉,这时系统会调用这个IBinder之前通过linkToDeath注册的DeathRecipient类对象的binderDied函数。

一般实现中,Bp端会注册linkToDeath,目的是为了监听绑定的Service的异常退出,一般的binderDied函数的实现是用来释放一些相关的资源。

posted @ 2015-11-13 18:52  maxiongying  阅读(1104)  评论(0编辑  收藏  举报