I'm making some service calls from background running IntentService, but it is not waiting for service call to return. After my research, it seems there are only two options, 1. Either use Service instead of IntentService 2. Make sync http call instead of async. Is there any way, I can make async http call from IntentService and listen the http callbacks ?
-
used `Service` for `Httpcall` in background – M D Oct 16 '15 at 11:06
3 Answers
IntentService is a fancy way to host a callback (onHandleIntent) inside a background thread inside a service.
Once the onHandleIntent returns (not waiting your async call to get back with the result), the intentservice shuts down.
In order to make it work you should add another layer of synchronization (such as a mutex) but it would waste the simplicity of the intentservice.
What I would try to do is to make the http call synchronous, or move the call to a service (if you really need to make that call inside the service and not from a frontend component like your activity or fragment).

- 6,834
- 3
- 27
- 34
Make a synchronous call to your http backend from an IntentService. The IntentService itself is already running on a background thread, so it makes sense for it just to wait for your backend call to complete.

- 2,341
- 1
- 12
- 19
I am not sure if this is what you want, but if you want to execute some IntentService work and while that do http operation then you can use Executor-s for that:
ExecutorService executor = Executors.newSingleThreadExecutor();
then inside IntentService handler:
Future<?> fut = executor.submit(new Runnable() {
// here do your network operations
});
// here do some other stuff inside IntentService handler
// wait for your http operation
fut.get();

- 48,511
- 9
- 79
- 100