Quick question to understand backgrounding

So from everything ive gathered background apps makes them not work, but I made an app which does nothing but listen to data on the bluetooth radio inside of a alwaysfires timer and if data is available it takes that data (which is a link) and does web.get with it. It seems to be running even when I use my app switcher and go to another app, and also when the phone screen goes off?

Am I just wrong and this is not my app working in the background? I am very very pleased with this behavior but also confused.

This is the code that i feel is working in the background..

My question is how is this possible? Maybe someone can explain it a bit :S ?

1 Like

Hello @Horacio_Spinelli your app will always run if it is open. As soon as you close it though, it will stop. Background apps aren't possible right now in app inventor. Background apps should always run even if the app is not open.
Since this does not run when it is not open, it is not a background app.

But the above code is running even when my screen is off or when another app is open, which is why im asking how :confused:

Yes, that is because the app is still running.
So, in a way it is running in the background.

Hmm, wow, so now I'm extra confused but pleasantly so.

I expected my app to instantly stop working when the screen goes off, and also when I swap apps. I thought that the android os automatically backgrounded those apps. I still am confused though, but thank you, that helped.

1 Like

Sorry...
Let's start from the beginning.
When you open any app, android basically marks it as open.
Now, when you press the home button, it minimizes it. It does it just like a computer.


It is still running, just it is minimized. Now, when you press the square button on your phone, it shows all the apps that are open right now. If your app is still there, it is still open and will keep on running.
After you get it, it is actually pretty simple.
Sorry again... :grimacing:

Oh, I thought the the square button showed all the backgrounded apps. And I thought minimized meant backgrounded.

Just out of curiosity when does the app get backgrounded then?

And thank you.

So did I. I don’t know what background means if not that.

Not anymore.
There is a paid extension for this which costs $25.
But if have knowledge of services then you can create one from yourself for free.

Android Documentations say that Background tasks or services means those long running tasks which are not dependent on the parent app's current state i.e. running , minimized or closed.
A background service can even start the parent app anytime even at boot which we often call Auto Start/Run at startup.

3 Likes

"Android Documentations say that Background tasks or services means those long running tasks which are not dependent on the parent app's current state i.e. running , minimized or closed."

Sounds like my above bluetooth code. So will android eventually kick my app into the background (the one running the above bluetooth code) and disconnect my bluetooth? I havnt tested to see how long the connection remains but if it does kill my code i would be down to pay 25 for an extension that backgrounds stuff. Do you know if the 25$ extension works nice?

Hey I actually just saw your guys' posts today dated a few months back where you asked/gave advice on extension making. I am very grateful you made that post because it made me start making a extension (its fun!!). I just finished one that's kind of useful so I would enjoy learning about services like you mention, any advice where to start?!

BY THE WAY, THANK YOU VERY MUCH MAN!!

I think my code runs in the background (in the literal sense) until android detects it as such?, at which point it backgrounds it in the android sense?, at which point my above code would break?

Meaning im kinda wrong when I say the above code works in other apps and when screen is off... but maybe im wrong because my code works with the screen off for as long as ive tested it.. which has been atleast 5mins i think.

Sorry, I have never tried that.

Android can't send your app in background.It can only minimize your app where it will continue to work until it goes in idle mode.
Running -> onPause -> onStop
A mimimized app is in paused state but it is stopped once you clear it from recents apps list.

I see thanks.

Hmm, I just woke up today and the code I pasted above was still running, it makes me happy because I want it to always run, but what has me extra confused is my app wasn't even in running this time, It wasn't even listed when I clicked the the square button. I actually turned off the phone to make it stop working just because I was surprised, and sure enough when the phone turned off the Bluetooth code i pasted stopped running.

Im really really happy and hope it always works like that, but once again confused how its possible that the above code was running this time when the app was not even appearing in the the app switcher (square button).

So, I've tried this many times too, and here is what happens. When you close your AI2 app, it stays in memory and continues to run. If you do nothing else overnight or maybe longer, your app will still be running. However, if you use a few other apps after closing your AI2 app, your AI2 app will no longer be running. Basically, the Android OS will keep your AI2 app running as long as it does not need resources (mostly memory) for anything else. But if you start opening other apps, Android will soon need the resources taken by your AI2 app and Android will then close it to free up these resources.

There is no way, right now, to tell Android to not close the app to free up the resources (except perhaps for this paid extension). It will always do so. I hope that this explanation makes the behavior clear.

1 Like

Wow thank you so much for clearing my head up about this. Your explanation was really good, It will allow me to toy with things further. If you know more about the backgrounding extension, I don't actually need it but I still am curious and will probably end up getting it if its good.

I finally feel I understand it. Cheers!

I have no idea about what you are asking, but I can pass a little tip I learned about backgrounding. You can easily make an app run all the time, even when its screen goes out of scope but making the application timer based. Meaning include a timer method enable the timer and then execute the code from the WhenTimer block. And the set the flag for always fire to true. Remember that once you enable the timer it will call the When timer block after the timer interval that has been programmed.


This helped me...

If you set the flag always fire

to true, it will run all the time.

To clarify some things that are claimed here:

  1. The BackgroundTask extension from Atom_Developer works (tested).
    But it has no influence on Doze mode.

  2. A background service (task) does not prevent an app from activating Doze mode as soon as the screen is off (idle / sleep mode). Then certain resources (network, WiFi, LTE, Bluetooth, timer (clock component), CPU ...) are gradually shut down.

  3. To prevent the Doze mode from becoming active in idle mode (screen off), a foreground service and a (partial) wakelock must be used.

See e.g. here:

No, unfortunately not on all devices. On a Galaxy Note8 or S8 (Android 9) in idle mode (screen off) the timer stops after a short time (a few minutes).

1 Like

I received this feedback in another thread, posted it here in case it was of use to somebody, but after testing it in my OnePlus 7 Pro phone, discovered it doesn't work in my phone. I just bought the Atom Developer extension for background tasks and will test it.