I have an IntentService
that downloads data from a server and I would like the IntentService
to check for server updates at a certain interval. The following posts however advice against repeating a Service
using a Timer
- and instead emphasize on using an AlarmManager
:
Why doesn't my Service work in Android? (I just want to log something ever 5 seconds)
Android - Service: Repeats only once
From Android's reference manual, an IntentService is described as:
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
The part I don't really understand is why an IntentService
(the posts have questions that are directed towards a Service
and not an IntentService
) is not allowed to execute repetitively using a Timer as it creates its own worker thread for execution. Is it permissible to use a Timer
within an IntentService
? Or are AlarmManagers
the only solution to periodically execute an IntentService
?
An explanation to this would be most appreciated .