How does an Alarm-app on the phone keep running in the background?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

in my mobile app I’m working on, a counter is continuing to count up. Putting it into onscreen == MainLoop.NOTIFICATION_WM_FOCUS_OUT: and later back to „focus in“ again, on Windows 10 the counter did not interrupt but shows the „right“ number, so I assume it just continued running in the background. On Android, however, it interrupts, resuming counting from where it focussed out (which is too bad…).
I’d be very interested if someone could tell me why Windows doesn’t interrupt.

However, my main question on that note is, naturally: how does an alarm-app on the phone keep running in the background?
These you can (assumingly?) close down completely after setting an alarm (or a timer…), but something has to continue running in the background so it can fire up at the right time.

Is there any way to have a Godot-made program do the same?

  • how does an alarm-app on the phone keep running in the background?

They probably hook directly into the operating system because they’re programming in an android app developing environment. There are a lot of things you can do while programming an android app natively that you can’t do when exporting a Godot game to android.

  • Is there any way to have a Godot-made program do the same?

Unless someone else knows otherwise, I highly doubt it.

timothybrentwood | 2021-07-26 14:25

Alright, that makes sense. Thanks for your answer!

pferft | 2021-07-26 14:28

:bust_in_silhouette: Reply From: Wakatta

The fact than your mobile device is attached to a battery, each Android app goes through a life cycle to ensure optimal power and memory (ram) usage.

So the system kills apps or places them in a dormant state for this reason simulating multiple tasks switching

Desktops have no such limitations and as such chrome can suck all the resources it wants while you run other apps in the foreground

What both OS’s have in common though are services and the very reason your alarm can go off even if your phone is switched off. In fact Google has about 8 constantly active ones collecting data

You’ll need to familiarize yourself with the Android services framework

And one can probably be started using Godot’s OS.execute() with the am start -a android.intent.action.MAIN -n command (not tested) by using the custom build template

That makes sense as well, thanks for the insight.

I see that I would have to do some extra steps using focus in/out (maybe some update-calculations to bridge the time-gaps) when “returning back to the app” on the phone…

Services! Already intimidating ; ) But thanks for the hint, maybe I can get into this a bit.

pferft | 2021-07-27 08:10

Yes yes this answer was in relation to your query.

A less complicated solution would be to record the time at focus out then at focus in subtract the current time from the recorded one

counter = current_time - focused_out_time

The downside to this approach would be time glitches and incorrect clocks

Wakatta | 2021-07-28 21:39

This is exactly my approach!

In that wake, is it right that OS.get_datetime() goes down to (only) seconds whearas OS.get_system_time_msecs() to, well, milliseconds? I can’t have the datetime accuracy down to msecs, can I?
Actually, I’d like accuracy running on tenth 0.0 of a second… would I have to calculate that out of system_time? (But couldn’t out of datetime?)

pferft | 2021-07-29 07:31