Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop an IntentService?

I use an IntentService to handle large file-downloads in my app. But when i want to cancel the download i call stopService(intent). However the onDestroy() method is been called but the download doesn't stop. What else do i have to do to cancel the download? Thanks.

like image 580
Philipp Redeker Avatar asked Mar 31 '11 16:03

Philipp Redeker


1 Answers

I have not figured this out myself, but I send an Intent with:

intent.putExtra("pause", "yes");
startService(intent);

And then I override public int onStartCommand(Intent intent, int a, int b) inside the IntentService (which is not recommended) and seeing that "pause" is "yes" I set a static boolean mPaused to true. If you have a loop in onHandleIntent, add && !mPaused in the loop condition.

Of course this is all 'bad' code and a hack... but I hope somebody else chimes in with the correct way of doing this.

like image 79
albnok Avatar answered Oct 01 '22 18:10

albnok