Could anyone tell me how to stop intentservice
in Android?
If I use stopservice
to stop an intentservice, it doesn't work.
I have browsed the website, but nobody has given a clear answer to this question.
Work is submitted to the queue by creating an Intent and then passing that Intent to the StartService method. It is not possible to stop or interrupt the OnHandleIntent method IntentService while it is working.
You can add a shutdown() method into your AIDL interface, which allows an Activity to request a stopSelf() be called. This encapsulates the stopping logic and gives you the opportunity to control the state of your Service when it is stopped, similar to how you would handle a Thread .
The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness.
If the task doesn't require any and also not a very long task you can use service. 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.
Try calling stopSelf()
from within the service. The documentation says that some processes may take a long time to complete, so your service is most likely waiting for them to stop. You should probably look into that. Once all requests are complete, the service stops itself.
The IntentService
is built to stop itself when the last Intent is handled by onHandleIntent(Intent)
- i.e. you do not need to stop it, it stops it self when it's done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With