Android Question Starting a service every 15 minutes.

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I would like to wake up a service in 15 minute intervals and continue to do that constantly. For example, if the user pressed a button to start it and the current time is 11:05, I would like the service to wake up at 11:15, 11:30, 12:00 and so on.

I was thinking about maybe using multiple StartServiceAtExact statements but that would be a lot of them. Is there an easy way to set this up?

Please show some coding samples I would need.

Thanks.
 

udg

Expert
Licensed User
Longtime User
HI, Merry Xmas!
I couldn't test it, but IMO you should do some math in your NExtTime sub; something like:
B4X:
'TIME TO NEXT 15 minutes slot
Sub TimeToNext15 As Long
   Dim minutes As Int = DateTime.GetMinute(DateTime.Now)  'ex. 09:12 --> 12
   Dim gap As Int
   gap = 15 - (minutes Mod 15)
   Return DateTime.Now + gap * DateTime.TicksPerMinute
End Sub
....
Log(DateTime.Time(TimeToNext15))  'ex. 09:15
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Same as above, but here you pass the time to be used as the starting point for calculation as a parameter
B4X:
'TIME TO NEXT 15 minutes slot
Sub TimeToNextMinute(TimeNow As Long) As Long
   Dim minutes As Int = DateTime.GetMinute(TimeNow)  'ex. 09:12 --> 12
   Dim gap As Int
   gap = 15 - (minutes Mod 15)
   Return TimeNow + gap * DateTime.TicksPerMinute
End Sub

'call as
Log(DateTime.Time(TimeToNextMinute(DateTime.TimeParse("09:12:00"))))  'use the right time format set for your device

Enjoy
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Udg,

Works perfect.

Now to incorporate that with Peter's SetExactAndAllowWhileIdle and set up notifications and everything should be all set.

Thanks everyone for all the help.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…