Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 891
Mytheral Avatar asked Jan 13 '16 15:01

Mytheral


2 Answers

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.

like image 95
Radu Ionescu Avatar answered Sep 30 '22 23:09

Radu Ionescu


What you have to know about intent service:

"The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. "

don't implement the onStartCommand method, because in case you do, you have to call the stopSelf() or stopService(), when you are finished. If you don't override this method, then Android OS handles everything.

As @Radu Ionescu mentioned, the intentService stop working, when it's done with all the request arrived to it's onHandleIntent method.

IntentService creates a default worker thread by it's on, and executes everything what arrives to the onHandleIntent method.

Just add everything to it's queue or if you need to take longer time before adding two different actions, you can use Service, to prevent the IntentService self destruction.

like image 40
narancs Avatar answered Sep 30 '22 21:09

narancs