AIDL - Calling BindService on dynamic intent retur

2019-09-01 03:28发布

问题:

I have a set of applications I've created. One of these applications is an Activity that basically a "home screen", and the other applications all contain a bunch of services I've created.

In the main application Activity, I have a ListView that uses an ArrayAdapter that I am using to display a list of the class and package names of custom services I've created that are installed on the phone. Each of the displayed services are hosted in a different application, but all of these services implement the same simple AIDL interface that I have created.

interface IExService {
void execute(in Bundle parameters, in ICallback callback);
}

In the main application I've got a binder and service connection like this:

private IExService _service=null;

private ServiceConnection _serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        _service=IExService.Stub.asInterface(binder);
    }
    public void onServiceDisconnected(ComponentName className) {
        _service=null;
    }
};

Upon selecting an item in the list, I would like to bind to that chosen service. The code for selecting an item in the list looks like this, where the MyService object represents the selected service item:

private void executeService(MyService service) {

    boolean isBound = bindService(new Intent(service.getClassName()), _serviceConnection, Context.BIND_AUTO_CREATE);
_service.execute(params, callback);

In the above code, the intent will be formed from the class name held by the selected item . The class name, for example, may look like "com.example.exWidgetService.ExampleService". This service will be defined in the manifest file for that application in the following way:

    <service android:name="com.example.ExampleApplication.ExampleService">
        <intent-filter>
            <action android:name="com.test.HomeApplication.IExService"/>
        </intent-filter>
    </service>

Where ExampleApplication is an application I've created that hosts the service, and HomeApplication is the main menu application where the actual AIDL file is defined.

Here is my problem: When making the BindService() call, the call succeeds and true is returned that the service has been bound. However, the _service object is always NULL. So even though the service binds successfully (or so is returned) I cannot execute any service methods because the binder is null. The DDMS doesn't seem to show anything helpful, and no problems seem to be logged during the BindService() call.

Is it a problem that I have multiple applications implementing the same AIDL service? If not, is there a proper way to call a specific service implementation dynamically during runtime?

回答1:

Hard to say without full code and debug ;), so lets go step by step. Could you please verify two things:

  • in onServiceConnected method, binder parameter is null too or only _service is null? In case if binder is null:
  • in your service you can override method onBind(do not remember exact name) and check if you really got the request.


标签: android aidl