Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do if OS has killed an IntentService?

Let's say we have an implementation of IntentService, then we start it from Activity and then add several other Intents in a queue to IntentService for processing. So now we have first Intent under processing and the rest of Intents in a queue. Now let's imagine the OS kills our Application process because of understandable reason. The questions are:

  1. How to restart the IntentService?

  2. How to restore the queue?

Well, I know there is always a Service where you can override it's onStartCommand() and tell a Service what to do. But I need an IntentService because of its queue model.

like image 201
Eugene Avatar asked Oct 03 '22 01:10

Eugene


1 Answers

IntentService is not what you want to use if the queue of work needs to be persistent. If you send many Intents to an IntentService and then the process is killed by Android, there is no way to rebuild the queue. IntentService simply queues the Intents up using an internal Handler and these are not persisted anywhere. They just get lost.

If I were you I would just implement this myself. The code for IntentService isn't that large and you can look at it and use it as a model for your own Service. You'll want to write your queue to a database so that it is persistent. Return START_STICKY from onStartCommand() so that Android will automatically restart your Service if it is killed due to resource requirements.

like image 161
David Wasser Avatar answered Oct 13 '22 10:10

David Wasser