I had tested now the alarm with a timer. And there is the old problem again.
I created a timer in a service. Here is a simplified example.
#Region Service Attributes
#StartAtBoot: True
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
Dim Time as Timer
Sub Service_Create
Dim n As Notification
n.Initialize
n.Icon = Null
n.Vibrate = False
n.OnGoingEvent = True
n.SetInfo("Info", "Prevent from being killed", Main)
Service.StartForeground(0, n)
End Sub
Sub Service_Start (StartingIntent As Intent)
If Time.IsInitialized = False Then
Time.Initialize("tim", 1000) : Time.Enabled = True
Log("Restart service - Init")
Else
Time.Interval = 1000 : Time.Enabled = True
Log("Restart service - Reset")
End If
End Sub
Sub Service_Destroy
Log("Service was destroyed. Restart in 5 seconds.")
StartServiceAt("", DateTime.Now + 5 * 1000, True)
End Sub
Sub tim_Tick
Log("Timer ticked")
CheckForAlarm
End Sub
Sub CheckForAlarm
'Here is code for restart the timer.
'If the time > 30 minutes until the next wake point, then restart timer in 30 minutes.
'Else restart at the wake time
End Sub
Now is the problem, that the service will killed by the OS without raise the "Sub Service_Destroyed".
I think it depends from the device.
I don't understand this because I use START_STICKY and Service.StartForeground.
On Samsung Galaxy S1 with Cyanogenmod (Android 4.3) the service will destroyed nearly instantly. (Low System)
On Samsung Galaxy S5 with Android 5.0 it will work 2-3 times and then the service stop. (Good System)
On Emulator with Genymotion and Cyanogenmod it works without problems. (System no matters)
If the OS would call the destroyed function then I could restart the service. But I get no request whether the service was killed or not.
Maybe a mix between StartServiceAt and the timer can be the solution, but this is very complicated.
I think, I work now with the workaround, that the service recreated himself in every minute and proof the time. If the duration of sleep bigger then the planned time, the application proof for missed alarm events. So a maximum of 5 minutes the alarm raised later. That's not a good solution, but the only I can see.
If I find a better way I will post it here.
Thanks all.
David