Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to stop IntentService

Tags:

I'm using an IntentService to upload images to a server. My problem is that I don't know how/when to stop the service. When I call stopself() in onHandleIntent(Intent ..) all Intents which are waiting in the IntentService queue are removed. But I don't want to stop the service from an activity because I want to complete upload proccess even if my application is not running.

like image 245
malinjir Avatar asked Apr 20 '12 17:04

malinjir


People also ask

How do I disable IntentService?

To stop a IntentService, call the method stopService (Intent service). It request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it is stopped.

Does IntentService run on background thread?

IntentService runs on its own thread. It will stop itself when it's done.

What is the purpose of IntentService?

IntentService is an extension of the Service component class that handles asynchronous requests (expressed as Intent s) on demand. Clients send requests through Context.

How can we stop the service in Android?

You stop a service via the stopService() method. No matter how frequently you called the startService(intent) method, one call to the stopService() method stops the service. A service can terminate itself by calling the stopSelf() method.


1 Answers

My problem is that I don't know how/when to stop the service.

IntentService automatically stops itself when onHandleIntent() ends, if no more commands had been sent to it while onHandleIntent() was running. Hence, you do not manually stop an IntentService yourself.

When I call stopself() in onHandleIntent(Intent ..) all Intents which are waiting in the IntentService queue are removed.

Which is why you do not do that.

But I don't want to stop the service from an activity because I want to complete upload proccess even if my application is not running.

Then you let the IntentService stop itself.

like image 97
CommonsWare Avatar answered Oct 14 '22 22:10

CommonsWare