StartServiceAt (exactly time)

MaxApps

Active Member
Licensed User
Longtime User
Hi

I am trying to find out, how to start a service at the same time every day.
I have searched the forum, but can only find examples on, how to start the service in 24 hour intervals (StartServiceAt("", DateTime.Now + 60000 * 60 *24, True)).
If, for some reason the service start is delayed and the service is started at a later timer, for example 09:05:00 then the next scheduled start will be tomorrow at 09:05:00, meaning this could eventually make the schedule slide.

But I need the service to be scheduled at exactly 09:00:00 am every day.

Any suggestions?

Kind regards
Jakob
 

cmweb

Active Member
Licensed User
Longtime User
Hi,

I'm using this:

B4X:
StartServiceAt("",DateTime.Now + 59 * 1000, True)

I stored the target time in a file, the service reads that file into varible "targettime" and then the service checks:

B4X:
If DateTime.Time(targettime) = DateTime.Time(DateTime.Now) Then
'do some stuff
...

Works fine here...

Best regards,

Carsten
 
Upvote 0

MaxApps

Active Member
Licensed User
Longtime User
But this way you are still counting the time from now, which means, if the start is delayed, it will calculate from the delayed time.

I am thinking something more like
StartServiceAt("", DateTime.TimeParse("09:00:00"), True)
I have tried this, but that just makes the service start over and over and over.....
 
Upvote 0

MaxApps

Active Member
Licensed User
Longtime User
Yes.
As far as I can see, they are both based on DateTime.now, which is not an exact time, but the time, when it is asked for.
What I need is to start the service at an exact time and not after x hours passed.

Kind regards
Jakon
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
But this way you are still counting the time from now, which means, if the start is delayed, it will calculate from the delayed time.

In my example, it doesn't matter which time the service has been started.

The service runs every minute and checks every minute, if it is the time you defined to do something...

Best regards,

Carsten
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Yes.
As far as I can see, they are both based on DateTime.now, which is not an exact time, but the time, when it is asked for.
What I need is to start the service at an exact time and not after x hours passed.

Kind regards
Jakon

You havent looked close enough, I will post the code again here:

Here hours and minutes are the variables you need to set when you want to start the service everyday. Lets say you want to start it at 15:00 each day. Just set hours = 15 and minutes = 0 in the example below.

The variable NowDay gets the current day today. This is so that we can find out if we need to schedule the service for today or tomorrow depending if the time has passed.

CurrentTime is also calculated for the same reason.

Look at how NextRunTime is calculated. It is the currentday (or currentday+1) + the FIXED time you specified.

If you still dont understand it, try running it and studying it more closely.

B4X:
Dim tt as Long
tt = hours * DateTime.TicksPerHour + minutes * DateTime.TicksPerMinute 

Dim NowDay As Long 
NowDay = DateTime.DateParse(DateTime.Date(DateTime.Now))

Dim CurrentTime As Long 
CurrentTime = DateTime.Now - NowDay

If tt > CurrentTime Then
    'Time has not passed, schedule for today
    NextRunTime = NowDay + tt
Else 'Schedule for tomorrow
    NextRunTime = NowDay + DateTime.TicksPerDay + tt
End If

Log("NextRunTime:" & NextRunTime & " Date:" & DateTime.Date(NextRunTime) &  "Time:" & DateTime.Time(NextRunTime))

StartServiceAt("",NextRunTime,True)




In my example, it doesn't matter which time the service has been started.

The service runs every minute and checks every minute, if it is the time you defined to do something...

Best regards,

Carsten
cmweb, he explicitly specified he is not looking for this kind of behaviour.
 
Upvote 0

MaxApps

Active Member
Licensed User
Longtime User
thedesolatesoul

Ahhh, now i get it. Yes that works like a charm.
It is so logic, that I should have figures this out by my self.
We have a saying in Danish: You cant see the forest, when the trees are blocking your view :icon_clap:
Thank you.

cmweb
Yours also work, but I do not wan´t to run the service every minute.
But still, Thank you.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
thedesolatesoul

Ahhh, now i get it. Yes that works like a charm.
It is so logic, that I should have figures this out by my self.
We have a saying in Danish: You cant see the forest, when the trees are blocking your view :icon_clap:
Thank you.

cmweb
Yours also work, but I do not wan´t to run the service every minute.
But still, Thank you.

Sometimes you need a bulldozer to knock down those trees :p

In my example, it doesn't matter which time the service has been started.

The service runs every minute and checks every minute, if it is the time you defined to do something...

Best regards,

Carsten
cmweb,
just looked at your post again. This seems like quite a wasteful way of scheduling since you are waking up the service every minute! Isnt it better to wake it up only at the specified time.
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Yes, of course. Your solution is much better than mine. It works great. I will change that in my app.

There is only one problem: if it is a start-at-boot-service, the service will run after booting the device even if it is not the specified time.

I will have to fix that problem. Any ideas on that?

Best regards,

Carsten



Gesendet von meinem GT-N7000 mit Tapatalk 2
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Yes, of course. Your solution is much better than mine. It works great. I will change that in my app.

There is only one problem: if it is a start-at-boot-service, the service will run after booting the device even if it is not the specified time.

I will have to fix that problem. Any ideas on that?

Best regards,

Carsten
In that case you should detect the StartingIntent to make sure it was started at boot.
Look here: http://www.b4x.com/forum/basic4android-updates-questions/17707-service-_create.html#post109743

Something like:
B4X:
If StartingIntent.Action = "android.intent.action.BOOT_COMPLETED" Then

End If
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
Ok I'm going nuts here :)

I've tried a couple of solutions for starting a service every day at the same time, but I keep running into loops!
Can somebody _please_ post the complete code for a service that runs every day at 17:00 hours and writes something to the log?

I'm absolutely sure I'm doing something wrong, but I can't figure out what...
 
Upvote 0
Top