Alarm clock using B4A

ribber

Member
Licensed User
Hi @all,

did someone of you already created an alarm clock event using B4A. For example I want to create an alarm at 7.00 PM. I know about the possibility using services and timer but I want to create a real alarm in the android OS.

Thank you.
 

kaputo

Member
Licensed User
Hello,

I put same question in another post but I think that in this post is better.

Is there any posibility to include a text for one alarm ?

Thanks
 

susu

Well-Known Member
Licensed User
What you mean "text for one alarm"? Do you want to show a message when alarm fires?
 

kaputo

Member
Licensed User
Yes, when you create new alarm with original clock, you can put a description text for this alarm.
 

tcgoh

Active Member
Licensed User
I wrote a simple alarm base on Erel's code. Hope it can help :)
Hi susu,

Thanks for the alarm code which I'll use for my project.

But how do you STOP the alarm when the media player started playing the wav file?

I can place another button click "AlarmService.mp.stop", but I could not integrate into your "CancelAlarm" button.

Thanks
 

Mashiane

Expert
Licensed User
@susu

Thanks for the nice code, however I have a problem on my side. I have copied your code as is and is now sitting in a class module as per code below with some tiny modifications.

B4X:
Public Sub NextTimeRun(sHour As Int, sMinute As Int) As Long
    DateTime.DateFormat = "dd/MM/yyyy HH:mm:ss"
    Log("AlarmHours: " & sHour)
        Log("AlarmMinutes: " & sMinute)
        Dim today 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)
        TimerDate = DateTime.Date(alarm) & " " & DateTime.Time(alarm)
        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("The next site query will be performed after: " & NumberFormat(h, 2, 0) & " hour(s) and " & NumberFormat(m, 2, 0) & " minute(s)", True)
        TimeRemaining = NumberFormat(h, 2, 0) & ":" & NumberFormat(m, 2, 0)
        Dim isleepTime As Long = DateTime.Now + ((h*60*60*1000)+(m*60*1000))
        Log("SleepTime: " & isleepTime)
        Return isleepTime
End Sub
The thing is my code returns an incorrect "isSleepTime" each time. Some are negative values and in some instances the date time calculated is 2 hours after the alarm. Your code works well as it fires the alarm each time on the Main class. Can you please advise please?
 

Mahares

Well Known Member
Licensed User
The sample works just fine. You need the Dialogs (2.80) lib. If it still does not, then add this line:
Dim alarm As Long
 

anOparator

Active Member
Licensed User
Thanks, I see now that it's not service module friendly (I don't need any UI), am looking at DateUtils for the solution.
 

wmardian

Member
Licensed User
Dear Erel and Maharesh,
I downloaded the simple_alarm.zip above to try to learn service. I run the application and connected the IDE via wifi to my android, the application installed successfully, and i can open the timedialog to specify the time. When i click the startservice button there is nothing wrong, until the time alarm should appear, in the log i saw below error:


An error occurred:
(Line: 13) End Sub
java.lang.Exception: Sub service_start signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject ac.de.fg.alarmservice_subs_0._service_start() throws java.lang.Exception

Could you please help me to know what error is it?
i struggle to learn service, and create a simple app. Please see below:

This is the service module
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
   
          StartServiceAt("", DateTime.Now + 10 * DateTime.TicksPerSecond, True)
          ToastMessageShow("test service and toast",True)
   
   
End Sub

Sub Service_Destroy
StopService("")
CancelScheduledService("")
End Sub
and this is the activity module:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim noti As Notification

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("L1")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Button1_Click


    noti.Initialize
    noti.Light = False
    noti.Vibrate = False
    noti.OnGoingEvent = True
    noti.Sound = False
    noti.Icon = "icon"
    noti.SetInfo("test","tes","")
    noti.Notify(1)
   
    StartService(serviceHitung)

End Sub
is it a correct way to create a service application?
I also fail to see the toastmessage..

Your help is vey appreciated.
Best Regards
 

wmardian

Member
Licensed User
Dear Erel,
Many thanks for your prompt respond.
Please find attached. It is a test service, but i am fail to make it run.



Thanks and Regards
 

Attachments

wmardian

Member
Licensed User
Dear Erel, and B4A members forum,
I try to run the application, and ;
1. hoping the toast message will show up every 10 seconds. But the toast message does not show up.
2. Please advice, is the structure of the code correct? I doubt about the way i call and implement "startservice()" and "startserviceat()" syntax.

Your help is very appreciated.
Thanks in advance
 

Mahares

Well Known Member
Licensed User
I tested you app exactly as you have it. It works. The toast message appears every 10 sec as you have it programmed. The problem might be somewhere else. I defer it to some one else better qualified than I to help you.
 
Top