'How to keep Android screen on after an action

I could have sworn there's a simple line of code for this, but I can't find it right now.

In my app I am using a piece of custom hardware for input. I receive events (on BLE) but not as keypresses or mouse actions. What I want to do is that each action should keep the screen awake just as if I had touched the screen (so, whatever the current timeout is gets reset)

To be clear - I do NOT want to use

android:keepScreenOn="true"

since that leaves the screen permanently on. I also don't want to be running a bunch of wakelocks - I'm almost certain there is a simple command to reset the screen timeout - what is it?



Solution 1:[1]

If you don't want to use Wakelock, some options:

  • Use SnackBar to briefly display a pop-up message.
  • Use FCM to fire a high priority message notification.
  • Create a foreground service that runs while your BLE device is allowing user input. Or use WorkManager the same way.

A more severe option, which requires your app to be a device admin, is to use DevicePolicyManager to set the maximum time to lock, ideally saving previous value and restoring after user input is complete.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zen_of_kermit