Question

I'd like to create an AIDL service that returns, for lack of the correct terminology, "live" objects. that is, I would like something like this to work,

IFoo foo = myService.getFoo(x); // calls to myService service to get an IFoo
IBar bar = foo.getBar(y); // IPC to IFoo to get an IBar
IBaz baz = bar.getBaz(z); // IPC to IBar to get an IBaz

baz.setEnabled(false); // IPC to IBaz to modify the service's copy of IBaz

I expect this to be possible, but I can find a good example. The alternative is to do something like,

myService.setBazEnabled(x, y, z, false);

the former being a more OO approach, while the latter is more functional.

Was it helpful?

Solution

So long as IFoo, IBar, and IBaz are all defined via AIDL, that should work just fine.

OTHER TIPS

Providing a clear example of the suggestion in comment #2 from CommonsWare ...

first, define sub-AIDL interfaces to be returned from the main AIDL interface,

interface IMyService {
  IFoo getFoo();
}

IFoo should itself be an AIDL interface,

interface IFoo {
  ...
}

in your implementation of IMyService.getFoo(), construct a new binder, and return that, as an IFoo interface,

public class MyService implements Service {
  public class FooBinder extends IFoo.Stub {
    ...
  }

  public class MyBinder extends IMyService.Stub {
    @Override
    public IFoo getFoo() {
      return IFoo.Stub.asInterface(new FooBinder()); 
    }

  @Override
  public IBinder onBind() {
    return new MyBinder();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top