TimerAlwaysFires

Is there a way to let Timer Always Fire also when the app is in background?

update:
I am realizing now that also if the app is not in background the Timer stops firing after some minutes

App Inventor apps not really are able to run in the background...
what exactly are you trying to do?

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

the app should send some data to the server every minute, even if it's in background or the phone is sleeping

See here:

So what do you mean by background?

the first concept :slight_smile: app not visible.
but i have found that the app is not doing anything even if opened and in foreground, when the phone goes to sleep.
I have also seen that if i move the phone (Xiaomi 10 lite) the finger print sensor lights up and the timer fires, but the rest of operation in the timer are non executed (connection to the server etc)

you have to prevent your device from sleeping...

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

is there a way to prevent sleeping keeping the display off?

what you could do is to reduce the brightness of the display... the settings extension is able to do this

Taifun

I must try to find another way, or this will make my App very "unconventional".
Do you think that could be possible for some expert to develop an Ext to workaround this limit of App Inventor?

In idle (sleep) mode, certain functions are throttled (CPU) or stopped by the Doze mode (e.g. WiFi) on individual devices (Samsung Galaxy Note8, Android 9).

The only thing you can try is to disable battery optimization.

https://community.appinventor.mit.edu/t/streaming-radio-drop-out-technical-advice/1213/18?u=anke

Does it work in the background when your device is charging (connected via USB cable)?

1 Like

WOW YES, that's why i never found out this issue during the development

So you can be sure that this problem is caused by the Doze mode (throttling of the CPU).
Maybe IGNORE BATTERY OPTIMIZATIONS will help in your case.

1 Like

Great Anke!
Yes the problem seems solved ignoring the battery optimizations.
Let's hope it will work on as many devices as possible.
Thanks all! :wink:

On which device & Android version?

on Xiaomi mi 10 lite it seems working Android 10
on Samsung J5 it seems not working Android 9
I'm making more tests and trying some different things i will report later

1 Like

Some new issues is coming up.
On some device, the timer is firing after ignoring the battery optimization (i put a BEEP to understand this) but the connection of the app to the internet is missing (web.get gives the error unable to get a response).
Other Apps running in foreground like gmail, have internet connection and work normally, but my app in background just Beeps not being able to connect to the server.
Is there something else related to connection that goes to idle?

You can try to trigger the IGNORE BATTERY OPTIMIZATIONS every sec or (500 ms like I did),
see here (my blocks in Screen1):

Thank you Anke,
ok i will try this in few minutes.
For the moment i am testing an extension by Said_dev LaunchMyApp.WakePhone.
It works, but it's boring to see the phone swicthing on every minute. I don't know if reducing the brightness of the screen (like Taifun suggested) will help.

if you keep the screen on, this will drain your battery, to reduce the brightness only can help to drain the battery less fast...

Taifun

yes i see, but i am looking for something that doesn't look too much "like a workaround".

As Anke suggested I tried to trig the TaifunOptimization.Request before any connection and the connection worked every time the timer fired.
(I'm curious to know and I don't understand why this result is better than to use TaifunOptimization.Request only at start, maybe Taifun can explain?)

But the problem in this case is that the timer fires every minute in the beginning, after a while it fires after 2 minutes, then after 3, then 6 minutes... I am afraid it fires when other apps force the phone to wakeup and not because of my timer.

Till now the best solution seems waking up the phone every minute, but i should add at least a notification to update a message to give a meaning to the workaround, but i tried 2 or 3 extension for notification that didn't work on all the phones.
Can you suggest a good one that i can update every time i wakeup the phone?
Also is there any way to know if my app is in a status of fore/background and if the screen is on/off?
Maybe all these things together will solve my issue.