Android Question Starting the service at a specific time...

Roger C

Active Member
Licensed User
Longtime User
The task is to make a daily break in a timereport, when entering a new day, the time should be save for the old and a new event should be started for the next day.

This could be done when the user checks out; check if it started on a previous day and then register yesterday as an event and then create and save a new event for the day he checked out.

But I think (for closing each day correct, there might be need for running a report early in the day and the user might not have ended his shift yet) that I would try to have a service running at midnight and close the day before and start a new event.

What is the best way to set the service to start at midnight every day?

I have this code in Service_Start but are not sure that it will set 24 hours until next start. The date will probably be the same day...
Should I add 1 to the date or should I set "00:00:00"?
Can I always be sure that this Service will be called at exactly 23:59:59 every day without delays?

Are there better ways to run a service at an exact time?

B4X:
Dim lTime As Long
Dim start, endTime As Long

start = DateTime.now
endTime = DateTime.DateTimeParse(DateTime.date(DateTime.Now), "23:59:59")
lTime = Abs(endTime - start)

StartServiceAt("Midnight",lTime, True)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
StartServiceAt expects the actual time, not the difference.

B4X:
StartServiceAt("Midnight", DateTime.TimeParse("23:59:59", True))

Make sure to also start the service after boot.

If it must happen before the end of the day then it is probably better to set it to 23:30:00. It should wake up the device at this time, however manufacturers can do all kinds of things to improve the battery life.
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Ahh... That's right, if I read everything then it works better...

But then this line makes some trouble for me:
Time - The time to start the service. If this time has already past the service will be started now.

If the service runs at 23:59:30 and then I tell it to run next time at 23:59:30 then it will start again since it is the same day and the time has passed.
Or am I wrong here?

What is the timespan for the service not to run directly again?
I understand if it's scheduled once an hour, then it's 9:20, 10:20... but once a day, then the time is passed when you set the new time.
 
Upvote 0
Top