Timer and background

Hello all,

I am trying to make an simple app that is counting the time I am spending on the app and each time the app is running but I am doing something else (so my app is suppose to be on background) the app is suppose to tell me that I spend time outside the app.

To do this :

  • setup a timer with a count up of 1 sec and loop.

  • each time the timer fires I am incrementing a variable (count) by one. (so far so good :wink: )

  • the first time I am launching the timer I am keeping the seconds since 1970 in a variable. (init)

  • each time the timer fires I am doing the difference between init and my current seconds since 1970. So I am supposed to have the real time between the two events. If this difference is the same as my “count” variable (more or less) I consider that I was not using my and it was in background.

this is suppose to work because our apps do not “work” in background so the timer does not fire when the app is not running (running on background)
my issue is that the apps never behave the save way. Sometimes the count stops (which is good) but sometimes it is not.

Does anyone can help me on this ?
I extract a part of the app.

https://x.thunkable.com/copy/d1e49c11e82e96ed0858040aa78db2ba

1 Like

Android 11 allows apps to be in background (even those who lacks the facility) until the app manager decides to kill (freeze) it therefore you might not get accurate expectations.

1 Like

I forgot to say that I am on ios :frowning:

do you have any idea on how I can do this functionnality in a different way (even if I am not online :frowning: )

1 Like

The closest I know that HTML5 has visibility API which will tell you if the web page is active or not.

You may try this with the Web Viewer but don’t know if it will work inside of Thunkable.

1 Like