Stopping a service after a StartServiceAt

QRT

New Member
Licensed User
Longtime User
Very new user - convert from Google's AppInventor, which won't run background services. Very impressed with first B4A programs.

Trying to produce a service which reminds me to take a pill! Just getting started with it. Read the 'life cycle' document several times! Possibly still confused!

The 'stripped down' project is attached (with a reminder set at 30 seconds). The toast message indicates when the service fires. I'm using the emulator.

Question 1: I expect the UI button to stop the service, hide the notification and close the app. However, the service fires up again within 30 sec and is not closed. Any ideas what I'm doing wrong?

Question 2: The emulator keeps running the service as I am editing my code. I assume that it completely resets itself before loading a re-compiled app, so it couldn't be running a service from a previously compiled version at the same time as I am running the current version. Is this correct?

Many thanks for your help in advance.
 

Attachments

  • MinimalServiceTest.zip
    6.3 KB · Views: 388

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot directly cancel a scheduled StartServiceAt task. Instead you should use a process_global variable as a "cancel" flag and then in Sub Service_Start check the status of this flag. If it is true then immediately return without scheduling the next task, you can also stop the service by calling StopService("").
After setting this flag to true call StartServiceAt to start the service in a couple of seconds. This will cancel the other scheduled task.
 
Upvote 0

QRT

New Member
Licensed User
Longtime User
Ahh - just a few hours for me to get my head around your answer!!!

The penny dropped quickly about the idea of setting the flag when the button was clicked and using it in the service to stop the StartServiceAt.

The difficulty was chasing down why my flag wasn't being recognised.....

Studying the Forum led to the second 'light bulb moment' after you pointed out to another user that to access a process variable which has been declared in one module, from a different module, you have to put the declaring module name in front of it (eg main.Stop_Service_Flag).

Oh dear, such a steep learning curve when you start a new programming environment.

Much obliged for your help - no doubt, I will back!!

Best wishes
 
Upvote 0
Top