gtdevel
gtdevel

Reputation: 1513

Stopping an IntentService

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

Upvotes: 5

Views: 4046

Answers (3)

Andres
Andres

Reputation: 400

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.

Upvotes: 1

Joerg Simon
Joerg Simon

Reputation: 421

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

Upvotes: 3

Andrew C
Andrew C

Reputation: 1036

You should be able to call stopSelf();

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

Upvotes: 2

Related Questions