I tried to write a little alarm app by using service module.
Main activity code:
B4X:
Sub SetSchedule_ItemClick (Position As Int, Value As Object)
Dim start, now, atTime As Long
DateTime.TimeFormat = "HH:mm"
start = DateTime.TimeParse(Value) ' parse time from xml, format like 14:32
now = DateTime.Now
If start > now Then
atTime = start - now
Msgbox("It's OK", "Set schedule")
StartServiceAt(notischedule, atTime, False)
Else
Msgbox("Cannot set schedule in past time","Error")
End If
End Sub
Service module name "notischedule" code:
B4X:
Sub Process_Globals
Dim Noti As Notification
End Sub
Sub Service_Create
Noti.Initialize
Noti.Icon = "icon"
Noti.Light = True
Noti.Sound = True
Noti.Vibrate = True
Noti.AutoCancel = True
End Sub
Sub Service_Start
Noti.SetInfo("Hey, it's time now!", "Alarm", Main)
Noti.Notify(1)
End Sub
Compile ok but it doesn't work and there's no notification icon. What did I miss? Please help.
For example you can save a list with the required times in a file. Schedule the first time and remove it from the list.
Then in Service_Create you should again schedule the next time and remove it from the list.
Note that not all devices will wake up for a scheduled task after sleeping for a long time.
It seems more difficult than I thought. What if I start services (3, 4 or even 10) to check itself every minute, it will ring the alarm on scheduled time. Is it possible? Is it the best way to make an alarm?