Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 777
gtdevel Avatar asked Aug 22 '11 11:08

gtdevel


People also ask

How do I disable IntentService?

When user call startService() from activity, it doesn't create the instance for each request and it going to stop service after done some action in service class or else we need to stop service manually by using stopSelf(). This example demonstrates about Android stop specified IntentService.

Does IntentService run on background thread?

If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread.

Why IntentService is deprecated?

Provided since Android API 3, the purpose of IntentService was to allow asynchronous tasks to be performed without blocking the main thread. The deprecation is one of a number of steps introduced starting with Android 8.0 (API 26) to limit the actions that can be performed while an app is in the background.

What is the main difference between a service and an IntentService?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly.


1 Answers

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

like image 119
Joerg Simon Avatar answered Oct 15 '22 23:10

Joerg Simon