DEV Community

cometa-kaito
cometa-kaito

Posted on Originally published at tech.rebounder.jp

FLAG_KEEP_SCREEN_ON Not Working on Android: The Screensaver

The answer

It's the screensaver (Daydream).

FLAG_KEEP_SCREEN_ON and disabling sleep run on a separate track from the screensaver. It fires in the instant the flag isn't in effect, so it's the one thing left standing after you've blocked everything else.

adb shell settings put secure screensaver_enabled 0
adb shell settings put secure screensaver_activate_on_sleep 0
adb shell settings put secure screensaver_activate_on_dock 0
Enter fullscreen mode Exit fullscreen mode

And these settings reset on reboot. Stop before that step and you'll think it's fixed when it isn't.

The symptom

  • A display meant to stay on goes black after a while
  • The device itself is alive — processing keeps running
  • FLAG_KEEP_SCREEN_ON is already set
  • Sleep timeout is already extended
  • It still goes dark, and you can't predict when

The unpredictability is the tell. A timeout would produce a consistent interval, but the screensaver slips in during "the instant the flag isn't in effect," so there's no fixed reproduction condition to chase.

More than one path to black

This is the annoying part: there are multiple paths to the screen turning off. Blocking one leaves the others open.

# Disable HDMI-CEC power linkage (gets dragged down by another device's power state)
adb shell settings put global hdmi_control_auto_device_off_enabled 0

# Disable auto power-off on no signal
adb shell settings put global no_signal_auto_power_off 0

# Screen-off timeout
adb shell settings put system screen_off_timeout 2147483647

# Sleep
adb shell settings put secure sleep_timeout -1

# ★ Screensaver — the most likely culprit
adb shell settings put secure screensaver_enabled 0
adb shell settings put secure screensaver_activate_on_sleep 0
adb shell settings put secure screensaver_activate_on_dock 0

# Stay awake while powered
adb shell settings put global stay_on_while_plugged_in 7
Enter fullscreen mode Exit fullscreen mode

It's easy to fix screen_off_timeout and sleep_timeout and assume you're done, but the screensaver is a setting distinct from both of those.

A layer adb can't reach

If the device is a TV used as the display, there's one more layer. The set's own off-timer sometimes reads a firmware-level setting of its own, and ignores writes from adb.

We hit this on one model where the power kept cutting out at 16:30. settings list global showed an item like tv_timer_power_off_timer_values, but writing to it did nothing — the firmware kept preferring its own setting.

This layer can only be cleared from the device's own settings screen — usually somewhere under timer, power, or energy saving. Assuming adb covers everything stops you cold right here.

Reboot resets all of it

This is the part that's easiest to miss.

Values written with settings put disappear on reboot. A command you ran by hand is an "effective right now" fix, not a permanent one. If the device restarts overnight, the next morning you're back where you started.

To make it permanent, have the app itself reapply the settings on every launch. That requires granting a permission once, up front.

# Write access to Settings.Secure / Settings.Global
adb shell pm grant <package-name> android.permission.WRITE_SECURE_SETTINGS

# Settings.System (screen_off_timeout) goes through appops instead
adb shell appops set <package-name> WRITE_SETTINGS allow
Enter fullscreen mode Exit fullscreen mode

WRITE_SECURE_SETTINGS is a signature|privileged|development permission, but pm grant can still assign it to a regular app.

Confirming it actually took

Without the permission, the app catches the exception and does nothing. It doesn't crash, so a missed grant is easy to overlook.

Check the actual writes in logcat.

adb logcat -s <tag>
Enter fullscreen mode Exit fullscreen mode

If you see the value it actually wrote — something like sleep_timeout=-1 or screen_off_timeout=MAX — it worked. A line reading skip: means a SecurityException, i.e. a no-op with no permission granted.

The key point here: don't treat "no error appeared" as proof of success.

Four layers, in summary

Layer How to clear it Survives reboot?
Screensaver screensaver_* → 0 ❌ resets
Sleep / screen-off sleep_timeout / screen_off_timeout ❌ resets
CEC / no-signal off hdmi_control_* / no_signal_* ❌ resets
Device's own off-timer From the device settings screen ✅ persists

The top three layers reset on reboot, so you need something on the app side to reapply them. Only the bottom layer is out of adb's reach — but clear it once from the device UI and it stays cleared.

What happened after running the same kind of device long-term is written up in Running a Google TV as a Device-Owner kiosk for several months.


Originally published at tech.rebounder.jp.

Top comments (0)