Android Question B2A : How to generate 2 Timers alive even if screen is OFF

Hello,
I use since a long time lots of version of M$ dev software. Visual Basic v3 to Visual Studio v2019 (C and VB).
I wanted to make a dual Timer, for example (10min and 45min).
When I use 2 timers, it works, but I the phone is in sleep mode (Screen OFF), the software doesn't continue to provide timer interruption.

Do you know if I can use a special setting option to keep alive my software?
or
I must use a different way?
A way could be to add an agenda entry at the specific Date/Time to generate an Alarm.

Thanks you your help.

Laurent.
 

Pendrush

Well-Known Member
Licensed User
Longtime User
You need to create timers in foreground service. Foreground service is alive when screen is turned off if you use partial locking.
 
Upvote 0
Thanks Pendrush for your response,
But I'm new (just started yesterday), and I can't find where I must declare the foreground service.
Could you tell me where I must modify my source code, please?

B4X:
#Region  Project Attributes 
    #ApplicationLabel: 2 Timers
    #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.
    Dim stdTimer1, stdTimer2 As Int    ' values in seconds
    Dim tmr1, tmr2 As Timer    ' My 2 timers
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 ButtonTimer1,ButtonTimer2 As Button    ' buttons to start and stop timers
    Private EditTextTimer1, EditTextTimer2 As EditText    ' TxtBox to display time
    Dim Timer1Min, Timer1Sec, Timer1TotalSec As Int        ' values in Minutes, seconds, TotalSeconds before the end
    Dim Timer2Min, Timer2Sec, Timer2TotalSec As Int        ' values in Minutes, seconds, TotalSeconds before the end
    
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("LayoutTimer")
    stdTimer1= 10 * 60                            ' 10 minutes
    stdTimer2= 45 * 60                            ' 45 minutes
    EditTextTimer1.Text=Sec2Chrono (stdTimer1)    ' format time for timer1
    EditTextTimer2.Text=Sec2Chrono (stdTimer2)    ' format time for timer1
    Timer1TotalSec=stdTimer1                    ' prepare countdown
    Timer2TotalSec=stdTimer2                    ' prepare countdown
    tmr1.Initialize ("Timer1Loop", 1000)        ' change value every 1sec
    tmr2.Initialize ("Timer2Loop", 1000)        ' change value every 1sec
End Sub

Sub Timer1Loop_Tick
    ' timer tick
    Timer1TotalSec = Timer1TotalSec-1            ' -1 sec
    If Timer1TotalSec=0 Then                    ' the end
        tmr1.Enabled=False                        ' timer stopped
        ButtonTimer1_Click                        ' simulate Button1 clic to reset Timer
        Dim b As Beeper
        b.Initialize( 1300, 500)
        b.Beep                                    ' beep
    End If
    Timer1_ChangeText                             ' REFRESH countdown
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See this tutorial

 
Upvote 0
Top