Книга: Beginning Android
Implement the Interface
Implement the Interface
Given the AIDL-created server stub, now you need to implement the service, either directly in the stub, or by routing the stub implementation to other methods you have already written.
The mechanics of this are fairly straightforward:
• Create a private instance of the AIDL-generated .Stub
class (e.g., IWeather.Stub
)
• Implement methods matching up with each of the methods you placed in the AIDL
• Return this private instance from your onBind()
method in the Service
subclass
For example, here is the IWeather.Stub
instance:
private final IWeather.Stub binder = new IWeather.Stub() {
public String getForecastPage() {
return(getForecastPageImpl());
}
};
In this case, the stub calls the corresponding method on the service itself. That method, which simply returns the cached most-recent weather forecast for the current location, is shown here:
synchronized private String getForecastPageImpl() {
return(forecast);
}
Note that AIDL IPC calls are synchronous, and so the caller is blocked until the IPC method returns. Hence, your services need to be quick about their work.