I have a question regarding Android's binder. If I understood correctly, a Binder can be passed between processes. On the service side, there is a Binder object (the original object). On the client side (in separate process), there is a BinderProxy object which handles the AIDL data marshalling.
My question is - how do you check if two BinderProxy objects are equal? By equal I mean, they refer to the same Binder object in the service process. Can we use the equals() method? Do BinderProxy objects have the same memory address as the original Binder objects?
Thanks!
Edit:
In response to David Wasser's comment:
The reason why I need to do this is little complicated to explain, but let me see if I can explain it clearly --
The remote service offers an AIDL API (which the client gets by receiving the IBinder in the onServiceConnected). The AIDL API, among other things, contains two methods --
IBinder getInterface ( String interfaceName );
void releaseInterface ( IBinder binder );
The idea is that the client applications can request the remote service for binders for different interfaces. The interface implementations' dex code and their binders are loaded into the service dynamically, and not known beforehand. The service provides these 2 AIDL methods for the client apps to access those dynamically loaded interfaces' binders, based on the interface name.
So say there is App1 and App2. Each does 'binder1 = getInterface( "SomeInterface1" );' and gets a binder for that dynamically loaded interface 'SomeInterface1'. After using it, the apps release the interface binder by saying 'releaseInterface( binder1 );' so that the service can unload the interface's dex code and do some cleanup.
However, the interface dex code cannot be unloaded unless both App1 and App2 have released the interface binder. So, in the service, I maintain a reference count for each dynamically loaded binder. (Say a Map key{Binder} ==> value{reference count}). When an App does getInterface(), I increase the reference count. When it does releaseInterface(binder1), I should decrease the reference count. But, to do so, I need to check the whether binder1 is equal to the binder keys in the Map.
Note that the binder has been passed from service (original binder object) to the client app (a binder proxy returned by getInterface()) and back again to the service (as a parameter of releaseInterface() - is it still a proxy, or the original??).
It is possible for App1 and App2 to get different binder objects (different implementations of an interface) for the same interface name. That's why, I use the binder for releasing.
Hope this wasn't too confusing! Thanks for the help.
Kartik
From eLinux.org. Binder is an Android-specific interprocess communication mechanism, and remote method invocation system. That is, one Android process can call a routine in another Android process, using binder to indentify the method to invoke and pass the arguments between processes.
Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls. This interface describes the abstract protocol for interacting with a remotable object.
Binder IPC Framework in Android Framework enables a remote invocation of the methods in other processes. A client process communicate to another server process and can run the methods in other process as it is done locally and can get the required data from the server process.
The binder proxy object has a asBinder()
method that returns the underlying IBinder
object that can be used to check for underlying equality of between Binder Proxies.
I'm not familiar with the mechanism, but I see that this method is used inside the LocationManagerService.java
file within the Android source code to register/unregister ILocationListener
objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With