Android Question Background task with timer

ebasic

Member
Licensed User
Longtime User
Hello there

I´ve got a question

Let´s say I have a timer (Timer1) and a label (lblCountdown) and a button (btnStart) and the situation is the following:

1. open the app, timer is initialized
B4X:
Timer1.Initialize("timer1",1000)

2. click the start button, timer1 is enabled
B4X:
Sub Button1_Click
    Timer1.Enabled=True
  
End Sub


The code goes let´s say:

B4X:
Sub Timer1_tick
    Label1.text=Label1.text+1
    Dim n As Notification
    n.Initialize
    n.Vibrate=False
    n.Sound=False
  
    n.Icon = "icon"
    n.SetInfo("CountDown", Label1.text, "")
    n.Notify(1)
End Sub


Now when the counter counts up to 10,20, 60 or anything I set up I would like to play a sound, beep or anything, which it also does - IF the app is in foreground and IF the screen is ON(Sure I can set the screen to be awake inapp), the thing is as soon as I open a new app(messaging etc) the counter app doesn´t play sound or beep as soon it reaches 10,20,60.

I´ve tried to solve it with service but the best I achieved is to put app in foreground as soon I open any other app.

Thank you very much
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Move the code to the starter service.
If the desired result is reached in the timer tick event then you can call a sub in a activity to show the notification. CallSubdelayed (which in fact will start an activity).

You can for example use a blank activity which you´ll start (Callsubdelayed("MySub",mynotificationactivity).
On start the mynotificationactivity activity will show the notification and then finish itself (Activity.finish).
 
Upvote 0

ebasic

Member
Licensed User
Longtime User
Hello DonManfred, thanks for responding. I´ve tried the callsubdelayed and it stops working after 20-21 seconds (I´ve read on forum about the same issue)
-basically I need the counter to count also in background (when screen off and when the app is not in foreground), but that doesn´t happen. I´ve seen ServiceExample where it works but can´t happen to issue any beep or sound while it´s not in foreground.

Regards
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
when screen off and when the app is not in foreground
Use a service like i already told.
beep or sound while it´s not in foreground.
You need to start a activity for this (i already told this too).
A Service cannot play sound or beep.
 
Last edited:
Upvote 0

ebasic

Member
Licensed User
Longtime User
Thanks, I will try to do so (need to read more about services first) and update the thread according to results

Regards
 
Upvote 0
Top