How do I implement some functions in my App to make it work when the screen is locked?

I wanted to implement some functions in my App to make it work when the screen is locked but I see that it doesn't work.
I thought to play a silent audio in the background to make the app work. But it's funny, the inventor or Taifun player can be working with the screen locked, but I can't take advantage of that activity to keep my app doing things.
Wouldn't there be a way to do it with this feature of the player?

What things?

I want to play an audio after some time, using a clock (from inventor).

Which device and Android version?

I am now on Android 12. I finally got the option to set the Appinventor player to play an audio without sound to work, and that makes the device with the locked screen respond when a clock times out and can make the app work (play an audio list after a clock time). I guess it's not a very orthodox method, but I had read that there was no way to make an appinventor app work with the device's screen locked.

this thread originally is from February 2000 and things have changed meanwhile... I therefore will move the new question and corresponding answers into its own thread...

thanks to extensions and the updated annotations we now have also the possibility to do things in the background...

for example my Alarmmanager extension with notification and autostart is able to autostart your app at a given time and play a sound... this also works if the device is locked...


1 Like

Thank you for your response. At the moment it works like this, later I will look to see this solution that you tell me!

Where did you read that?
All of my apps run even when the screen is locked, for hours (all night). This has been working fine for more than 7 years on tens of thousands of devices. All apps play music / sounds and use one or more timers. So where exactly is your problem? Post a little test aia that can reproduce this problem.

1 Like

Sorry, I have been disconnected for a few days, I also needed it... hahahaha.
I have put here in this aia test how the application works. There is a playlist and in between silent pauses.
When the screen is active it works, but when it locks it stops playing the whole list, although there is a timer running all the time to try to keep the ap`p from stopping completely.
Just a small note: now the player shows error when loading the selected source, I must have made a mistake in some detail. I put the aia file and photo so you can see the blocks.

The problem is that when the screen locks the app does not complete the playlist. If it is at that moment playing an audio it does not stop, but when it enters in silent phase it does not finish the playlist.

The playlist has 2 audios and 2 silence slots with a clock. I tried it this way and it stopped with the screen locked in the mute phase. Then I tried to set another clock to run for the whole time to prevent it from stopping and to complete the playlist, but it didn't work.

The aia test was missing to ask for permissions to read the external storage. Here are the blocks and the .aia file:

Test_locked_screen.aia (79.6 KB)

@Anke please can you take a look and tell me how I could make the app work when the screen is locked.
Actually I solved it by putting another active player with an audio without sound, but I think there must be a better way?

Yes, use a Foreground service.

But what's the problem with using another player that plays a silent 5 sec long sound in a loop (since that solved your problem)?

1 Like

Well in the example list I only put a few seconds of silence, but in the app I have it can be configured with more time. For example I go to sleep and I want to wake up with a song.
It works well, but I think it must consume much more battery than some functionality of AI2.
If there are advantages with Foreground service, please can you give me some example blocks of how it works?

The advantage of a foreground service is that it prevents Doze mode.

Search the forums (AI2 and Kodular) and you will find it ...

1 Like

Ok, I´ll try with Background Tasks extension [3.8 A]