Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop one IntentService while there are many IntentService are running

I have a listview contains many songs. Each time a song is clicked, I call an IntentService to download this song by sequence.

But sometimes I want cancel a download (for example: the 5th). It means I need to stop the 5th running IntentService. I attempt to stop it by call stopService() but it doesn't work.

Can anyone tell me a good way to stop IntentService? Thanks so much for your help.

like image 460
anticafe Avatar asked Dec 12 '22 16:12

anticafe


1 Answers

There is only one running IntentService, per <service> element in your manifest. If you call startService() six times, at most one IntentService will be running. Commands will queue up and be processed one at a time by onHandleIntent(), if the IntentService is processing a command when another command is sent.

In your case, since you cannot readily cancel an outstanding command with IntentService, you will probably need to create your own version of IntentService, where you create your own Service subclass with its own work queue, one where you have the ability to cancel commands that are not yet running.

like image 171
CommonsWare Avatar answered Dec 14 '22 06:12

CommonsWare