I have an IntentService
that starts an asynchronous task in another class and should then be waiting for the result.
The problem is that the IntentSer
I agree, it probably makes more sense to use Service
directly rather than IntentService
, but if you are using Guava, you can implement an AbstractFuture
as your callback handler, which lets you conveniently ignore the details of synchronization:
public class CallbackFuture extends AbstractFuture
AbstractFuture
defines get()
which blocks until the set()
or setException()
methods are called, and returns a value or raises an exception, respectively.
Then your onHandleIntent
becomes:
@Override
protected final void onHandleIntent(Intent intent) {
CallbackFuture future = new CallbackFuture();
MyOtherClass.runAsynchronousTask(future);
try {
Object result = future.get();
// handle result
} catch (Throwable t) {
// handle error
}
}