Alarm using timedialog

mkvidyashankar

Active Member
Licensed User
Longtime User

Erel

B4X founder
Staff member
Licensed User
Longtime User
This should help you get started:
B4X:
Sub Process_Globals
   Dim alarmHours, alarmMinutes As Int
End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)

End Sub
Sub Activity_Click
   Dim td As TimeDialog
   td.Hour = alarmHours
   td.Minute = alarmMinutes
   If td.Show("Select time", "", "Ok", "Cancel", "", Null) = DialogResponse.POSITIVE Then
      alarmHours = td.Hour
      alarmMinutes = td.Minute
      Dim today, alarmTime As Long
      today = DateTime.DateParse(DateTime.Date(DateTime.Now)) 'Sets today at 12:00 AM
      alarm = today + td.Hour * DateTime.TicksPerHour + td.Minute * DateTime.TicksPerMinute
      If alarm < DateTime.Now Then alarm = DateTime.Add(alarm, 0, 0, 1)
      Log(DateTime.Date(alarm) & " " & DateTime.Time(alarm)) 'check the logs to see the set date and time
      'Calculate time to alarm
      Dim h, m As Int
      h = Floor((alarm - DateTime.Now) / DateTime.TicksPerHour)
      m = ((alarm - DateTime.Now) - h * DateTime.TicksPerHour) / DateTime.TicksPerMinute
      ToastMessageShow("Time to alarm: " & NumberFormat(h, 2, 0) & ":" & NumberFormat(m, 2, 0), True)
      StartServiceAt(AlarmService, Alarm, True)
   End If
End Sub
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Hi Erel

I have a problem

when i use the time dialog and i set the time for example to 12:01
Then its saved as 12:1 and not 12:01
How can i fix this?

Sorry because of my bad English :)

Thanks in andvance :)
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi,
You just need to use the 'Numberformat,
B4X:
btnBlockOnTime.Text = NumberFormat(td.Hour, 2, 0) & ":" & NumberFormat(td.Minute, 2, 0)
 
Upvote 0

kaputo

Member
Licensed User
Longtime User
Hello, Is there any posibility to put a text into this alarms, when you put a alarm into the OS clock you can do it but with this function I cannot find it.

Thanks
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Erel,

how is the AlarmService and the Alarm declared ?
AlarmService should be service, but how and which one ?

I created a button "TimerButton" and change the first line of your sampe to the following:
B4X:
Sub Timerbutton_Click

When i hit it a error occurs: activity must be initalized first, but i did this above with
B4X:
AlarmService.Initialize("AlarmService")
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
If i not DIM it in any way an error occurs:

B4X:
Error description: Undeclared variable 'alarmservice' is used before it was assigned any value.
Occurred on line: 753
        StartServiceAt(AlarmService, alarm, True)

I DIMed it as it, so only Dim AlarmService !
The variable alarm i DIMed as Long.

After that, the popup appears and ask for the time :) (btw: can i set it to 24h format ?)

I read some other threads about StartServiceAt and this one looked good:
B4X:
Sub AlarmService_Tick
Msgbox("Alarmtime","Hint")
End Sub

But when the alarm time is arrived, nothing happens.
 
Last edited:
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Hi Erel. I have used your code and placed in a class module but its always returning minus time in the toast message . What could I be doing wrong?

B4X:
Public Sub NextTimeRun(sHour As Int, sMinute As Int) As Long
     Dim today, alarm As Long
      today = DateTime.DateParse(DateTime.Date(DateTime.Now)) 'Sets today at 12:00 AM
      alarm = today + sHour * DateTime.TicksPerHour + sMinute * DateTime.TicksPerMinute
      If alarm < DateTime.Now Then alarm = DateTime.Add(alarm, 0, 0, 1)
      Log(DateTime.Date(alarm) & " " & DateTime.Time(alarm)) 'check the logs to see the set date and time
      'Calculate time to alarm
      Dim h, m As Int
      h = Floor((alarm - DateTime.Now) / DateTime.TicksPerHour)
      m = ((alarm - DateTime.Now) - h * DateTime.TicksPerHour) / DateTime.TicksPerMinute
      ToastMessageShow("Time to alarm: " & NumberFormat(h, 2, 0) & ":" & NumberFormat(m, 2, 0), True)
      Return alarm
     'StartServiceAt(AlarmService, Alarm, True)
End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Hi Erel. I have just done a check. Your code is actual correct. Thanks, The problem happens when I open the TimeDialog control and set a new alarm date. My system is recording alarm times in a database in HH:mm format. When the app starts it checks the times and allocates a sleeping time for each of the alarms that have been set.

This is what happens...

1. I compile the app to my phone.
2. the apps starts and reads the alarms and runs your code and produces this log..
3. the sleep time for each alarm is updated as these need to run everyday same time.

B4X:
' this log is working perfectly..
Table_GetCursor: SELECT * FROM SiteIntervals
2:36
07/24/2013 02:36:00
Alarm: 1374626160000
UpdateRecord: UPDATE [SiteIntervals] SET [SleepTime] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimerDate] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimeRemaining] = ? WHERE [ID] = ?
5:20
07/23/2013 05:20:00
Alarm: 1374549600000
UpdateRecord: UPDATE [SiteIntervals] SET [SleepTime] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimerDate] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimeRemaining] = ? WHERE [ID] = ?

Now I open a TimeDialog to set an alarm, now the calculations become gooblygook.
When a new alarm is added, turned off or deleted, the app must re-rerun the alarms settings applying new sleeping times and turn on the next alarm.
The problem is after I open the timedialog to add a new date. It seems like the tickcounts get confused. Any suggestions. Below is the log after I add a new date. 5:30 becomes 5:48 and 2:36 becomes 2:54 with no dates.

B4X:
Table_GetCursor: SELECT IntervalHour,IntervalMinutes,AlarmTime,SleepTime,TimeRemaining FROM SiteIntervals WHERE OnOff = 'Y' AND AlarmTime >= '05:18' ORDER BY AlarmTime
* open timedialog and create new alarm
5:30
48 05:48:00
Alarm: 100080000
InsertMaps (first query out of 1): INSERT INTO [SiteIntervals] ([ID], [SiteID], [AlarmTime], [OnOff], [IntervalHour], [IntervalMinutes], [SleepTime], [TimerDate], [TimeRemaining]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ExecuteCSV: SELECT ID, AlarmTime, OnOff from SiteIntervals where SiteID = '125045' order by AlarmTime
Table_GetCursor: SELECT * FROM SiteIntervals


2:36
54 02:54:00
Alarm: 89640000
UpdateRecord: UPDATE [SiteIntervals] SET [SleepTime] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimerDate] = ? WHERE [ID] = ?
UpdateRecord: UPDATE [SiteIntervals] SET [TimeRemaining] = ? WHERE [ID] = ?
 
Upvote 0
Top