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
phomm
March 20, 2026, 1:47am
2
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
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 ?
PowerManager.WakeLock wakeLock;
final long WAKELOCK_TIMEOUT = 10*60*1000L; // 10 minutes
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// [START android_backgroundwork_wakelock_create_java]
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyClassName::MyWakelockTag");
wakeLock.acquire(WAKELOCK_TIMEOUT);
// [END android_backgroundwork_wakelock_create_java]
super.onCreate(savedInstanceState);
}
@SuppressWarnings("unused")
// [START android_backgroundwork_wakelock_release_java]
void doSomethingAndRelease() throws MyException {
try {
phomm
March 21, 2026, 9:40am
6
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:
procedure KeepScreen(AEnable: Boolean);
begin
{$ifdef DEBUG}
if AEnable then
WritelnLog('keep-screen is ON')
else
WritelnLog('keep-screen is OFF');
{$endif}
{$if defined(ANDROID)}
if AEnable then
Messaging.Send(['keep-screen', 'ON'])
else
Messaging.Send(['keep-screen', 'OFF']);
{$endif}
end;
end.
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 It’s part of my packages plan .
1 Like