8

Could anyone tell me how to stop intentservice in Android?

If I use stopservice to stop an intentservice, it doesn't work.

I have browsed the website, but nobody has given a clear answer to this question.

6 Answers 6

5

Try calling stopSelf() from within the service. The documentation says that some processes may take a long time to complete, so your service is most likely waiting for them to stop. You should probably look into that. Once all requests are complete, the service stops itself.

Sign up to request clarification or add additional context in comments.

4 Comments

Could I stop it from an activity?
Like I said, if stopService doesn't help, a process is hogging it up. protected abstract void onHandleIntent (Intent intent) Since: API Level 3 This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call stopSelf().
Do you mean If I call stopService, an intent will be send to intentservice. If intentservice already have several intent on the queue, "stopservice intent" will not be handle until those intent are handled?
In fact, I just find that if you call stopSelf() within your intentService when there is still intents waiting in the service queue, the waiting intents wont get execute. So, I don't think you should call stopSelf() unless you want your service stop immediately after it finishes its work.
5

The IntentService is built to stop itself when the last Intent is handled by onHandleIntent(Intent) - i.e. you do not need to stop it, it stops it self when it's done.

Comments

1

According to the documentation Android stops the service after all start requests have been handled, so you never have to call stopSelf().

Comments

1

Belated reply in case someone has the same issue. If you call <yourContext>.stopService(intent) where intent is the same intent object you used to start the service then it will work.

Comments

1

An IntentService is designed to stop itself only when all the requests present in the work queue have been handled.As per docs,IntentService class "Stops the service after all of the start requests are handled, so you never have to call stopSelf()"

Comments

0

Intent services automatically stop with

stopSelf()

only when all the requests present in the work queue have been handled.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.