Android Question StartServiceAt stops after Restart

Jonathan Rigley

Member
Licensed User
Longtime User
Hi,

If I run the following code and wait, it will run and fire after 5 mins. But if I restart the phone it does not fire.

B4X:
StartServiceAt(MyService, DateTime.Now + 300 * 1000, True)

If I add #StartAtBoot: True and restart then the service fires as soon as the phone as restarted and not in 5 mins time.

How can I restart the phone and still have the service fire at the right time and not as soon as the phone starts?

Thanks
 

udg

Expert
Licensed User
Longtime User
But if I restart the phone it does not fire.
This should be expected since after the device restarts there's nothing that loads again that servrice.
If I add #StartAtBoot: True and restart then the service fires as soon as the phone as restarted and not in 5 mins time.
With StartAtBoot you ask the OS to start the service after a device's boot, so it's up to you to discriminate between "first run" and subsequent ones. You may use a boolean var and set it after first run. So the service start, you set the var and do nothing. After 5 min the service starts again, you read the var, know that's time to do something and execute the code. This will happen again and again every 5 mins.
Is it that what you're planning?

Edit: if the service is not kept active (foreground) eventually it will be killed by the OS and if the user launces it manually it will skip the first run as for a restart.

udg
 
Upvote 0

Jonathan Rigley

Member
Licensed User
Longtime User
Thank you for your reply.

The application is a MOT and Insurance reminder so I pick a date in the future and a time and the app fires up on that date. It works until the phone is restarted.


This should be expected since after the device restarts there's nothing that loads again that servrice.

With StartAtBoot you ask the OS to start the service after a device's boot, so it's up to you to discriminate between "first run" and subsequent ones. You may use a boolean var and set it after first run. So the service start, you set the var and do nothing. After 5 min the service starts again, you read the var, know that's time to do something and execute the code. This will happen again and again every 5 mins.
Is it that what you're planning?

Edit: if the service is not kept active (foreground) eventually it will be killed by the OS and if the user launces it manually it will skip the first run as for a restart.

udg
 
Upvote 0
Top