Android Question Stopping a service that's rescheduled to restart

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I'm calling a service from an activity. That activity has a button to start the service and another button to stop it. In the called service I'm using SetExactAndAllowWhileIdle to reschedule the service so it continues to run. Same thing happens when using StartServiceAtExact from within the same service.

The user also wants to tap the stop button to stop that service and not allow it to reschedule itself until the start button is tapped. In my code I call StopService and it does stop the service but it still wakes up at the scheduled time.

Can you tell me what coding I need to cancel the scheduled part of the service as well? Seems like there may be some sort of queue that needs to be emptied first through coding.

In the mean time I think I will set some flag to stop the code in the service from executing based on the button tapped.

Thanks.
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Are you using the starter service?

Dont forget to use Service.StopForeground if you are using Service.StartForeground.

Are you sure that StopService iis being called when you are tapping on the button. You should put a break point at the StopService to make sure that it's being called.

Also look at
B4X:
Sub Service_Destroy
'Place your code here......
End Sub
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Peter,

It's not the starter service.

Thanks for the tip on using Service.StopForeground. Didn't know about that one. I will let you know how it works later.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks. I just added it into the code for the button. I will let everyone know later how it goes.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks for all of your help. The app is now running quite smoothly now.
 
Upvote 0
Top