Android Question Countdown Alarm

pojeck

Member
Hi Guys
I developed an andorid application and released in googleplay
Generally users are happy with my app but they want and need new properties like alarm option.

1607936331085.png


This is my app and i need to set an alarm when countdown is 00:00:00 (hours/minutes/seconds)
How can i do it?

Could you please help me friends?
Thanks.
 

pojeck

Member
you can use a service to play the alarm sound when the countdown is at 0 (if the app is in the background).
Could you please give me more specific examples because i really dont understand StartServiceAtExact
Thank you very much for your support.
 
Upvote 0

pojeck

Member
B4X:
Sub Process_Globals
    Dim gDevice As Phone
    Private gPhone As PhoneVibrate
    Dim gRTM As RingtoneManager
    Dim gSound As String
End Sub

Sub PlaySound
    gSound = gRTM.GetDefault(gRTM.TYPE_NOTIFICATION)
    gRTM.Play(gSound)
    gPhone.Vibrate(1000)
End Sub

I found this code and it's working but how can i use this code at exact time.

1608042882561.png


my app will be in background and i want to play sound or send a message
I would be very glad if anybody can help.
Thanks.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
there are so many tutorials in this forum. you really should start reading some and you will find very fast the answer.
start with this one to understand how service works https://www.b4x.com/android/forum/threads/service-modules.7542/#content


it could be something like this (Not Tested!!!):

main module:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
    
    Dim parseDateTime As Long = DateTime.DateTimeParse("15/12/2020","20:15:05")
    StartServiceAtExact(playalarm,parseDateTime,True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

now add a service object to your app, call it "playalarm" (Project -> Add New Module -> Service Module")

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    Dim gPhone As PhoneVibrate
    Dim gRTM As RingtoneManager
    Dim gSound As String
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    playalarmnowAndShowNotification
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub playalarmnowAndShowNotification
    gSound = gRTM.GetDefault(gRTM.TYPE_NOTIFICATION)
    gRTM.Play(gSound)
    gPhone.Vibrate(1000)
    
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Alarm","Weak Up!", Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

code is not tested!!
 
Upvote 0

pojeck

Member
there are so many tutorials in this forum. you really should start reading some and you will find very fast the answer.
start with this one to understand how service works https://www.b4x.com/android/forum/threads/service-modules.7542/#content


it could be something like this (Not Tested!!!):

main module:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
   
    Dim parseDateTime As Long = DateTime.DateTimeParse("15/12/2020","20:15:05")
    StartServiceAtExact(playalarm,parseDateTime,True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

now add a service object to your app, call it "playalarm" (Project -> Add New Module -> Service Module")

B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    Dim gPhone As PhoneVibrate
    Dim gRTM As RingtoneManager
    Dim gSound As String
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    playalarmnowAndShowNotification
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub playalarmnowAndShowNotification
    gSound = gRTM.GetDefault(gRTM.TYPE_NOTIFICATION)
    gRTM.Play(gSound)
    gPhone.Vibrate(1000)
   
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Alarm","Weak Up!", Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

code is not tested!!

Hi
Actually your code is working perfectly, but i want to create 5 different alarms and i tried this code

B4X:
Sub Button1_Click
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
    
    Dim parseDateTime As Long = DateTime.DateTimeParse("17/12/2020","07:59:05")
    StartServiceAtExact(PlayAlarm,parseDateTime,True)
    
    Dim parseDateTime As Long = DateTime.DateTimeParse("17/12/2020","08:00:05")
    StartServiceAtExact(PlayAlarm,parseDateTime,True)
    
    Dim parseDateTime As Long = DateTime.DateTimeParse("17/12/2020","08:01:05")
    StartServiceAtExact(PlayAlarm,parseDateTime,True)
    
    Dim parseDateTime As Long = DateTime.DateTimeParse("17/12/2020","08:02:05")
    StartServiceAtExact(PlayAlarm,parseDateTime,True)

End Sub

It's just executed last one (08:02:05)
How can i create different alarms? Why previous alarms not worked?

Thanks.
 
Upvote 0

pojeck

Member
The other think is this code is working perfectly with galaxy s3mini, samsung a51 but when i try with Redmi Note 8 it's not working, why?
For example i changed this code like below

B4X:
    Dim parseDateTime As Long = DateTime.DateTimeParse("19/12/2020","20:30:00")
    StartServiceAtExact(PlayAlarm,parseDateTime,True)

Samsungs are worked service
But Redmi is not.

Any idea?
 
Upvote 0

pojeck

Member
You cannot schedule the same service multiple times. You need to schedule the nearest time and then make the next schedule each time.

In this case it is better to schedule the first time and then keep the app running with a foreground service.

Hi Erel, thank you so much for your kindly replay
I have written code like below, do you think it's good code?

This is Main
B4X:
#Region  Project Attributes
    #ApplicationLabel: Ezan'ı Test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#BridgeLogger: True

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText
    Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    If in.HasExtra("Notification_Tag") Then
        Log(in.GetExtra("Notification_Tag")) 'Will log the tag
        MsgboxAsync("Bildirim Çubuğundan tıklanarak geldi","Başlık")
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"

    Dim parseDateTime As Long = DateTime.DateTimeParse(DateTime.Date(DateTime.Now),EditText1.Text)
    StartServiceAtExact(Uyari,parseDateTime,True)
    MsgboxAsync("Tamam","Ok")
    
End Sub

Sub Button2_Click

    Try
        If Uyari.MediaPlayer1.IsPlaying Then
            Uyari.MediaPlayer1.Stop
        Else
            Msgbox("Alarm Çalmıyor","Boş")
        End If
    Catch
        Log(LastException)
    End Try
    
    StopService(Uyari)
    
End Sub

This is Service
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private nid As Int = 1
    Dim MediaPlayer1 As MediaPlayer
    Dim AlarmAudio As String
    Private lock As PhoneWakeState
End Sub

Sub Service_Create
    Log("Servis created")
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)
    Log("Servis Started")
    Service.StartForeground(nid, CreateNotification("..."))
    PlayAlarmNowAndShowNotification
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_HIGH)
    notification.Icon = "alarm"
    notification.Light = True
    notification.Vibrate = True
    'notification.AutoCancel = True 'tıklayınca iptal olması için.
    notification.SetInfo2("Its Time","Please Do your Job","Notification_Tag", Main)
    Return notification
End Sub

Sub PlayAlarmNowAndShowNotification
    MediaPlayer1.Initialize
    AlarmAudio = "skypealarmtone.mp3"
    MediaPlayer1.Initialize
    MediaPlayer1.Load(File.DirAssets,AlarmAudio)
    MediaPlayer1.Looping = True
    MediaPlayer1.Play
End Sub

Sub Service_Destroy
    Log("Servis Destroy Oldu")
    lock.ReleasePartialLock
    'StartServiceAt(Me, DateTime.Now + 1000, False)
End Sub

This code is working good but for REDMI (Xiamoi) Phones i think i need more permission. Because it's not worked for this phone. I added manually permission from Application information > Automatic Startup > True
and i changed Notifications settings like below and code is WORKED like other phones.

Screenshot_2020-12-20-23-20-56-133_com.android.settings.jpg
Screenshot_2020-12-20-23-21-06-707_com.android.settings.jpg


Can i get more permissons for this phone? or what i need to do?

I think i can handle this, found some examples.
You cannot schedule the same service multiple times. You need to schedule the nearest time and then make the next schedule each time.

Thank you very much for your support.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code with Msgbox cannot be considered good code. Other than that I don't see any issue.

Can i get more permissons for this phone? or what i need to do?
There is nothing that you can do. Many manufacturers don't allow apps to run in the background or put all kinds of unique restrictions.
 
Upvote 0

pojeck

Member
Code with Msgbox cannot be considered good code. Other than that I don't see any issue.


There is nothing that you can do. Many manufacturers don't allow apps to run in the background or put all kinds of unique restrictions.
Hi Erel
Thank you very much for your support.
I hope i can do what i think :)
 
Upvote 0
Top