5

Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?

Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?

Thanks

gtdevel
  • 1,513
  • 6
  • 21
  • 38

3 Answers3

3

bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals("Stop"))
        stopSelf();
    onStart(intent, startId);
    return START_NOT_STICKY;
}

cheers

Joerg Simon
  • 421
  • 2
  • 7
2

You should be able to call stopSelf();

http://developer.android.com/reference/android/app/Service.html#stopSelf()

Andrew C
  • 1,036
  • 2
  • 9
  • 19
  • yes but how would you tell the service to call `stopSelf`? the message would get queued up – james Sep 13 '11 at 21:06
1

I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.

Andres
  • 400
  • 3
  • 8