简体   繁体   中英

How to stop an android Background IntentService?

My requirement is simple.

I have a simple app. 2 Buttons. and 1 service. Conditions: Button1 clicked,-> Start the IntentService.Button2 clicked -> Stop the IntentService.

The first part works fine. I can start the IntentService using startService(intent) command. However, I cannot stop the IntentService. ie, stopService(intent) command isn't working. It is neither throwing an error nor doing anything.

Can anyone please shed some light on this??

I searched and found this question How to stop an IntentService? but it doesn't answer my need.

You can't stop an IntentService manually. An IntentService will stop itself automatically, when there are no more intents for that service to process.

If you want to stop a service by clicking a button, use a normal Service .

I figured it out. We cannot stop an IntentService till it completes all the steps in the process.

However, we can place an intermediary check Ex: if(boolean continue == true){then do next}.esle {stopSelf(); } And we can change the value of 'continue'to false and instead of 'StopService' command.

Hope it helps someone who has similar problem like me.. :-)

However, you cannot kill the IntentService . The service will stop automatically after all of thread queues completed. Though you call MyIntentService.this.stopSelf(); from Activity , the service will stops, but the thread still running in background process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM