Search code examples
androidandroid-intentandroid-intentservice

Android - Running a IntentService multiple times


So I have a IntentService that runs fine when I run it a single time. It takes an image manipulates it then outputs a series of RGB values.

But what I now need it to do is run multiple times to batch out a series of images. My first attempt involved calling stop service in my main class then creating and running a new instance of the IntentService. But this crashed when I called StopService.

Is there a correct way to do this?


Solution

  • The IntentService stops the service after all requests have been handled, so you never have to call stopSelf().

    The IntentService cannot run tasks in parallel and all the consecutive intents will go into the message queue and will execute sequentially.

    So, just add them one after the other and be sure to clean-up your fields to ensure all Intents are handled independently since the IntentService object/thread is not recreated.