Android: How to prevent а device from going to sleep?

Hello to All!

I’m making a workout timer for Android. For such an app, it’s crucial that the screen doesn’t turn off while the counting is in progress.

So my first question is: how can I prevent the screen from turning off automatically?

And a related question: how can I keep the counting process going when the app is minimized?

1 Like

Most probably you would need to add a new android service with specific permission

But I don’t know the exact details..

1 Like

At first glance it looks very complicated, because I don’t know either this programming language and this API at all -______-

But thanks for the tip anyway :sweat_smile:

1 Like

Indeed, I’m afraid both tasks you ask about require Android-specific code, using Java to access Android APIs, links from @phomm answer are exactly the ones I would recommend. Mobile applications, out of the box, just don’t have such “power” by design – a regular application cannot keep your screen from turning off, and regular application may be killed at any moment by Android.

2 Likes

Studying this example and the CGE code, I’m starting to understand something…

What does “MyClassName::MyWakelockTag” mean on line 21?

as said in the docs, it is “just” a tag, so mostly for programmer, just a convenient string for you. there is a good practice to name it based on class+methodname https://developer.android.com/develop/background-work/background-tasks/awake/wakelock/best-practices

I say “just” , bc it is somewhat used by system for logging and statistics

2 Likes

Ok, I made it just a screen hold like for a video player. It doesn’t even require permission.

Here is the service I wrote:

Here is Pascal usage:

According to this documentation:

This functionality is already enough to make my app fully usable. And I can make the release.

PS: For Windows version added same functionality.

PS2: Background counting isn’t implemented yet, but I thinking on it.

2 Likes

Thanks for sharing, and the service is nicely simple! I’m happy to see more people are making use of our Android services.

P.S. Ability to define Android service in a project and not need to copy it to CGE sources (to address also your notes in training_timer_2/android_services/readme.md at main · SerufuYua/training_timer_2 · GitHub ) will come. I know the current approach is not comfortable :slight_smile: It’s part of my packages plan.

1 Like