Timer scomparso automaticamente ─ countdown (not) working in background

Hi everyone,
I basically set this timer for 1 hour, and then, after opening other apps, I simply left the phone with the screen off. At a certain point, I went to pick it up from among the other running apps, and to my dismay, I realized that the timer was no longer running. :upside_down_face:
TermosifoniTimer2.aia (899.5 KB)

You should really know by now that this would happen. Once the app moves to the background the clock will stop.

You can counter this by capturing the timer position when the clock stops, save this to tinydb. When you bring the app to the foreground get the current time and do maths to set the timer accordingly.

This extension can help you:

1 Like

I know very well that the timer will stop in the background... but when I went to resume the app, the timer obviously started again. But after 1 hour, I saw that the timer had actually been disabled.

1 Like

It is possible that your app closed, or that you timer ran out of time, and turned of the clock.

Here is a simple example using tinydb and a seconds procedure.

countdownWithTinydb.aia (4.6 KB)

and this one will take account of the elapsed time when the clock is stopped (emulates "keeps counting down")

countdownWithTinydbElapsed.aia (5.6 KB)

2 Likes

Hmm, how would that help? The timer stops after a short time if the app is in background/idle mode. When the app comes back to the foreground, the timer resumes where it leaves off.

We've discussed these Doze issues dozens of times in the last 6 years. You need a foreground service to keep the timer active. Period.

Therefore as @TIMAI2 already tried to explain, you have to check the lifecycle of your app

My tools extension offers an ActivityStateChanged event for this App Inventor Extensions: Tools | Pura Vida Apps

If state = stop, save the timer position to TinyDB
If state = resune, recalculate the timer position and continue counting from there

No need for a foreground service

Taifun

If saving "time" and "counter" to tinydb (as in my second example) - the countdown remains "current" when the app is resumed.

I would save a deadline for the alarm.

Compare now against the deadline.

It's self correcting across loss of control.

That doesn't help either, at least not if the timer/countdown has already expired.

To keep a countdown timer active in the background, a foreground service must be used. Otherwise, you couldn't even use a countdown app to boil an egg.

1 Like

I created the two APKS and set the countdown time for both to 1 hour:
CowntdownWithTinydb (Countdown1)
CowntdownWithTinydbElapsed (Countdown2)
These are the results:


First one looks about right

Second one you probably need this

I'd save the end time in the database, and in the timer compare the current time with the end time

1 Like

Everything depends on the requirements for your countdown timer

If you want to see a notification displaying the current timer while the app is not running, use a foreground service: itoo extension together with melon notification extension

If you do not need to display the current timer while the app is not running but you need a notification if the countdown finished, use an alarmmanager. The free simple alarmmanager extension already can do it and of course there also is my paid alarmmanager extension with more features.

Else follow @TIMAI2 's advice, see again my previous answer

Taifun

2 Likes

This is all you need:

I finally relaunched the countdownWithTinydbElapsed APK, and set the time to 1 hour; it managed to get to the end, resuming every time I brought it back from its background state.

1 Like

On review it needs more work!

Not on my test device (Pixel 7 Pro, Android 16).

My phone is: Samsung A16 android 16

The countdown stops after just a few seconds (< 1 min). I play a ticking sound every second.