I'm creating an app that reads every Service that has an IntentFilter that matches a custom action by using this:
Intent intent = new Intent(ACTION);
List<ResolveInfo> infos = getActivity().getPackageManager()
.queryIntentServices(intent, 0);
I have no problem retrieving the Services, but when I try to bind them to the current Activity, my IBinder implementation is not passed to onServiceConnected(ComponentName, IBinder)
. Instead, a BinderProxy
is being passed.
How can I get my LocalBinder
from that BinderProxy
?
EDIT:
Here's my implementation:
public abstract class LocalService extends Service {
private LocalBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public LocalService getServiceInstance() {
return LocalService.this;
}
}
@Override
public final IBinder onBind(Intent intent) {
return mBinder;
}
public abstract List<Category> getInitialCategories();
public abstract void onObjectSelected(Item object,
LocalCallback callback);
}
What I want to do is to be able to call the last 2 methods from other people's implementations of my LocalService.
I don't think you can do that.
You're binding to a service in a different process. It would be impossible to get a reference to your actual LocalBinder
instance, because processes cannot share object instances.
I haven't tested this scenario myself, but according to the documentation if you need IPC for Services you have two alternatives, either a Messenger or AIDL. The first one seems much simpler.
If you need your interface to work across different processes, you can create an interface for the service with a Messenger. In this manner, the service defines a Handler that responds to different types of Message objects. This Handler is the basis for a Messenger that can then share an IBinder with the client, allowing the client to send commands to the service using Message objects. Additionally, the client can define a Messenger of its own so the service can send messages back.
This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that you don't have to design your service to be thread-safe.