Android Question Schedule service correctly

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hi all ,

I have two times like 5 AM and 6PM a service should start on
Every time this service starts it checks and schedule itself to the coming time .

I am trying this but it does not work some times

B4X:
Sub Schedule(hs As Long,ms As Long)

        Dim alarm As Long

        DateTime.TimeFormat="HH:mm:ss"
        Dim today As Long
        today = DateTime.DateParse(DateTime.Date(DateTime.Now)) 'Sets today at 12:00 AM
        alarm = today + hs * DateTime.TicksPerHour + ms * DateTime.TicksPerMinute + DateTime.TicksPerMinute
        If alarm < DateTime.Now Then alarm = DateTime.Add(alarm, 0, 0, 1)
        Dim h, m As Int
        h = Floor((alarm - DateTime.Now) / DateTime.TicksPerHour)
        m = ((alarm - DateTime.Now) - h * DateTime.TicksPerHour) / DateTime.TicksPerMinute
        CancelScheduledService("")
        StartServiceAt("", (DateTime.Now+((h*60*60*1000)+(m*60*1000))), True)
    End Sub
I am changing time so I test it but sometiimes it just does not start
Please help
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
You should use DateUtils.AddPeriod to calculate the correct time.

Call StartServiceAtExact instead of StartServiceAt. Which version of Android are you running?
You are right I should use Exact as I have Android 5.1
But I am having a difficulty understanding how datetime works.
If possible can I have a simple example ?
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Even changing time would start service .
Is there any fix ?

EDIT : I found the reason .
There is a missing minute so If for example I pass 12:34 FindNextTime would return ticks that represents 12:33
 
Last edited:
Upvote 0
Top