The approach of using linkToDeath
is the only one which I know of.
Normally here's what I would do:
- Have a class,
IMySystemService.aidl
which is implemented by your system service.
- Have a listener,
IMySystemServiceListener.aidl
which is implemented by each client.
- Add a method such as
IMySystemService.addListener(IMySystemServiceListener listener)
Then, use linkToDeath
on the listener within your system service to get a callback for when each client dies. You can then clean up the relevant parts of your cache.
If your project is at all complex already, you probably have some suitable per-client listener already?
Can you answer why linkToDeath
doesn't work for your design?
One common pattern for caches is to use WeakHashMap for your cache - but this only works if you have a key object which is garbage-collected when the client disappears. I believe that this, in turn, can only be achieved using linkToDeath
so we're back to where we started!