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.

Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
user1127497
  • 81
  • 1
  • 1
  • 2

6 Answers6

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.

Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
  • Could I stop it from an activity? – user1127497 Jan 03 '12 at 09:17
  • 1
    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().` – Tomislav Markovski Jan 03 '12 at 09:19
  • 2
    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? – user1127497 Jan 03 '12 at 09:30
  • 1
    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. – Chen Jan 21 '14 at 18:59
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.

Jens
  • 16,853
  • 4
  • 55
  • 52
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.

Nonos
  • 2,450
  • 2
  • 23
  • 34
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()"

Shinoo Goyal
  • 601
  • 8
  • 10
1

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

Mus
  • 1,860
  • 2
  • 16
  • 19
0

Intent services automatically stop with

stopSelf()

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

Rahul
  • 3,293
  • 2
  • 31
  • 43