Sub Service_Start (StartingIntent As Intent)
Dim Fix, M20, M40, NextHour As Long
Dim Now As Long = DateTime.Now
Dim Per As Period
Per.Hours = 1
Dim NextRun As Int
Fix = DateUtils.SetDateAndTime(DateTime.GetYear(Now), DateTime.GetMonth(Now), DateTime.GetDayOfMonth(Now), DateTime.GetHour(Now), 0, 0)
M20 = DateUtils.SetDateAndTime(DateTime.GetYear(Now), DateTime.GetMonth(Now), DateTime.GetDayOfMonth(Now), DateTime.GetHour(Now), 20, 0)
M40 = DateUtils.SetDateAndTime(DateTime.GetYear(Now), DateTime.GetMonth(Now), DateTime.GetDayOfMonth(Now), DateTime.GetHour(Now), 40, 0)
NextHour = DateUtils.AddPeriod(Fix, Per)
If Now >= Fix And Now < M20 Then
NextRun = DateUtils.PeriodBetween(Now, M20).Minutes + 1
Else If Now >= M20 And Now < M40 Then
NextRun = DateUtils.PeriodBetween(Now, M40).Minutes + 1
Else If Now >= M40 Then
NextRun = DateUtils.PeriodBetween(Now, NextHour).Minutes + 1
End If
Dim TimeToRun As Long
TimeToRun = DateTime.TimeParse(DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now) & ":00") + NextRun * DateTime.TicksPerMinute
StartReceiver(NTF)
StartServiceAtExact(Me, TimeToRun, True)
Service.StopAutomaticForeground
End Sub