Hi all,
i have a problem with start a alarm through a service. The service should check every minute for the wake time, but it will not work. I tested it under Android 5.0, Android 4.3 (Cyanogenmod) and Android 4.3 (Tab3). On every device the same problem.
At first I tried it with a Timer in service :
Works fine. But if the device are sleeping (screen off), the timer stops until the user awake the device.
Then with StartServiceAt :
Works not steadily.
I use the Service Attribute
In the event Service_Destroy :
The code :
Here is a log file :
The alarm was not raised because in the alarm time the service don't checked for the time. But the service should checked the time every 44 seconds. I tried to check with more then 44 seconds. But the same result.
I know, this way is not the best for check the time. But in this example I only want to know why the service proof not every minute.
Any suggestion what matters ?
Thank you.
David
P.S. Sorry for my english
i have a problem with start a alarm through a service. The service should check every minute for the wake time, but it will not work. I tested it under Android 5.0, Android 4.3 (Cyanogenmod) and Android 4.3 (Tab3). On every device the same problem.
At first I tried it with a Timer in service :
Works fine. But if the device are sleeping (screen off), the timer stops until the user awake the device.
Then with StartServiceAt :
Works not steadily.
I use the Service Attribute
#StartAtBoot: True
#StartCommandReturnValue: android.app.Service.START_STICKY
In the event Service_Destroy :
StartServiceAt(Me, DateTime.Now + 5 * 1000, True)
The code :
B4X:
Dim StartTime as String = "10:30"
Sub Service_Create
DateTime.TimeFormat = "HH:mm"
Dim n As Notification
'This part only prevent for killing the process through the OS
n.Initialize
n.Icon = Null
n.Vibrate = False
n.OnGoingEvent = True
n.SetInfo("Info", "Prevent from being killed", Main)
Service.StartForeground(0, n)
SaveInText("Service created.")
End Sub
Sub Service_Start (StartingIntent As Intent)
CheckForUpdate
End Sub
Sub CheckForUpdate
Dim tmpZeit As String = DateTime.Time(DateTime.Now) 'Nowtime
StartServiceAt("", DateTime.Now + (44 * 1000), True) 'Restart service in 44 seconds
'Check for alarm time
If tmpZeit = StartTime then
'Raise alarm
End If
SaveInText("Service restart in : 44 seconds.") 'Log
End Sub
'Write the log in file
Private Sub SaveInText(SaveText As String)
Dim tw As TextWriter
Dim Tt As String
tw.Initialize(File.OpenOutput(File.DirRootExternal, "Info.txt", True))
Tt = DateTime.time(DateTime.Now) & " : " & SaveText
Log(Tt)
tw.Write(CRLF & Tt)
tw.Flush
tw.Close
End Sub
Here is a log file :
09:00 : Service restart in : 44 seconds.
09:03 : Service restart in : 44 seconds.
09:04 : Service restart in : 44 seconds.
09:04 : Service restart in : 44 seconds.
09:05 : Service restart in : 44 seconds.
09:06 : Service restart in : 44 seconds.
09:08 : Service restart in : 44 seconds.
09:10 : Service restart in : 44 seconds.
09:10 : Service restart in : 44 seconds.
09:11 : Service restart in : 44 seconds.
09:12 : Service restart in : 44 seconds.
09:13 : Service restart in : 44 seconds.
09:15 : Service restart in : 44 seconds.
09:16 : Service restart in : 44 seconds.
09:17 : Service restart in : 44 seconds.
09:19 : Service restart in : 44 seconds.
09:21 : Service restart in : 44 seconds.
09:22 : Service restart in : 44 seconds.
09:23 : Service restart in : 44 seconds.
09:24 : Service restart in : 44 seconds.
09:24 : Service restart in : 44 seconds.
09:26 : Service restart in : 44 seconds.
09:27 : Service restart in : 44 seconds.
09:29 : Service restart in : 44 seconds.
09:33 : Service restart in : 44 seconds.
09:34 : Service restart in : 44 seconds.
09:35 : Service restart in : 44 seconds.
09:37 : Service restart in : 44 seconds.
09:39 : Service restart in : 44 seconds.
09:39 : Service restart in : 44 seconds.
09:40 : Service restart in : 44 seconds.
09:41 : Service restart in : 44 seconds.
09:42 : Service restart in : 44 seconds.
09:44 : Service restart in : 44 seconds.
09:44 : Service restart in : 44 seconds.
09:46 : Service restart in : 44 seconds.
09:48 : Service restart in : 44 seconds.
09:49 : Service restart in : 44 seconds.
09:50 : Service restart in : 44 seconds.
09:54 : Service restart in : 44 seconds.
09:55 : Service restart in : 44 seconds.
09:56 : Service restart in : 44 seconds.
09:57 : Service restart in : 44 seconds.
09:59 : Service restart in : 44 seconds.
09:59 : Service restart in : 44 seconds.
10:00 : Service restart in : 44 seconds.
10:01 : Service restart in : 44 seconds.
10:02 : Service restart in : 44 seconds.
10:03 : Service restart in : 44 seconds.
10:03 : Service restart in : 44 seconds.
10:04 : Service restart in : 44 seconds.
10:06 : Service restart in : 44 seconds.
10:08 : Service restart in : 44 seconds.
10:09 : Service restart in : 44 seconds.
10:11 : Service restart in : 44 seconds.
10:12 : Service restart in : 44 seconds.
10:13 : Service restart in : 44 seconds.
10:14 : Service restart in : 44 seconds.
10:15 : Service restart in : 44 seconds.
10:16 : Service restart in : 44 seconds.
10:17 : Service restart in : 44 seconds.
10:19 : Service restart in : 44 seconds.
10:20 : Service restart in : 44 seconds.
10:21 : Service restart in : 44 seconds.
10:22 : Service restart in : 44 seconds.
10:23 : Service restart in : 44 seconds.
10:24 : Service restart in : 44 seconds.
10:25 : Service restart in : 44 seconds.
10:25 : Service restart in : 44 seconds.
10:26 : Service restart in : 44 seconds.
10:27 : Service restart in : 44 seconds.
10:28 : Service restart in : 44 seconds.
10:29 : Service restart in : 44 seconds.
10:29 : Service restart in : 44 seconds.
10:32 : Service restart in : 44 seconds.
10:32 : Service restart in : 44 seconds.
10:33 : Service restart in : 44 seconds.
09:03 : Service restart in : 44 seconds.
09:04 : Service restart in : 44 seconds.
09:04 : Service restart in : 44 seconds.
09:05 : Service restart in : 44 seconds.
09:06 : Service restart in : 44 seconds.
09:08 : Service restart in : 44 seconds.
09:10 : Service restart in : 44 seconds.
09:10 : Service restart in : 44 seconds.
09:11 : Service restart in : 44 seconds.
09:12 : Service restart in : 44 seconds.
09:13 : Service restart in : 44 seconds.
09:15 : Service restart in : 44 seconds.
09:16 : Service restart in : 44 seconds.
09:17 : Service restart in : 44 seconds.
09:19 : Service restart in : 44 seconds.
09:21 : Service restart in : 44 seconds.
09:22 : Service restart in : 44 seconds.
09:23 : Service restart in : 44 seconds.
09:24 : Service restart in : 44 seconds.
09:24 : Service restart in : 44 seconds.
09:26 : Service restart in : 44 seconds.
09:27 : Service restart in : 44 seconds.
09:29 : Service restart in : 44 seconds.
09:33 : Service restart in : 44 seconds.
09:34 : Service restart in : 44 seconds.
09:35 : Service restart in : 44 seconds.
09:37 : Service restart in : 44 seconds.
09:39 : Service restart in : 44 seconds.
09:39 : Service restart in : 44 seconds.
09:40 : Service restart in : 44 seconds.
09:41 : Service restart in : 44 seconds.
09:42 : Service restart in : 44 seconds.
09:44 : Service restart in : 44 seconds.
09:44 : Service restart in : 44 seconds.
09:46 : Service restart in : 44 seconds.
09:48 : Service restart in : 44 seconds.
09:49 : Service restart in : 44 seconds.
09:50 : Service restart in : 44 seconds.
09:54 : Service restart in : 44 seconds.
09:55 : Service restart in : 44 seconds.
09:56 : Service restart in : 44 seconds.
09:57 : Service restart in : 44 seconds.
09:59 : Service restart in : 44 seconds.
09:59 : Service restart in : 44 seconds.
10:00 : Service restart in : 44 seconds.
10:01 : Service restart in : 44 seconds.
10:02 : Service restart in : 44 seconds.
10:03 : Service restart in : 44 seconds.
10:03 : Service restart in : 44 seconds.
10:04 : Service restart in : 44 seconds.
10:06 : Service restart in : 44 seconds.
10:08 : Service restart in : 44 seconds.
10:09 : Service restart in : 44 seconds.
10:11 : Service restart in : 44 seconds.
10:12 : Service restart in : 44 seconds.
10:13 : Service restart in : 44 seconds.
10:14 : Service restart in : 44 seconds.
10:15 : Service restart in : 44 seconds.
10:16 : Service restart in : 44 seconds.
10:17 : Service restart in : 44 seconds.
10:19 : Service restart in : 44 seconds.
10:20 : Service restart in : 44 seconds.
10:21 : Service restart in : 44 seconds.
10:22 : Service restart in : 44 seconds.
10:23 : Service restart in : 44 seconds.
10:24 : Service restart in : 44 seconds.
10:25 : Service restart in : 44 seconds.
10:25 : Service restart in : 44 seconds.
10:26 : Service restart in : 44 seconds.
10:27 : Service restart in : 44 seconds.
10:28 : Service restart in : 44 seconds.
10:29 : Service restart in : 44 seconds.
10:29 : Service restart in : 44 seconds.
10:32 : Service restart in : 44 seconds.
10:32 : Service restart in : 44 seconds.
10:33 : Service restart in : 44 seconds.
I know, this way is not the best for check the time. But in this example I only want to know why the service proof not every minute.
Any suggestion what matters ?
Thank you.
David
P.S. Sorry for my english
Last edited: