screen-state & screen-toggle¶
Two small Bash scripts (~80 and ~140 lines) to read and change the Android screen state idempotently. Designed for automation via SSH or cron.
Requires root
Both call dumpsys power (read) and input keyevent (action), which
require root via su -c or sudo.
screen-state¶
Reads the screen state and returns it as an exit code — for use in if
and &&/|| in scripts.
Usage¶
screen-state # prints "Tela Ligada" / "Tela Desligada"
screen-state -q # no output; just exit code
screen-state -v # also shows mWakefulness / mHolding
screen-state --help
Exit codes¶
| Code | Meaning |
|---|---|
0 |
screen on (Awake or Dreaming) |
1 |
screen off (Asleep or Dozing) |
2 |
couldn't tell (no root / dumpsys failed) |
64 |
invalid usage |
Examples¶
# classic if
if screen-state -q; then
echo "on"
else
echo "off"
fi
# one-liner
screen-state -q && echo "on" || echo "off"
# only act if needed
screen-state -q && screen-toggle --off -q
How state is read¶
The script calls dumpsys power and looks for mWakefulness=:
- Awake → screen on;
- Dreaming (screensaver / daydream) → counts as on;
- Asleep → screen off;
- Dozing (AOD / Doze mode) → counts as off.
If mWakefulness isn't visible (newer Android), it falls back to:
Display Power: state=ON/OFF/DOZE.
screen-toggle¶
Turn the screen on, off, or toggle, reading state before acting to be idempotent.
Usage¶
screen-toggle # toggle (default)
screen-toggle --on # turn on only if off
screen-toggle --off # turn off only if on
screen-toggle --toggle # explicit (same as no argument)
screen-toggle -q # quiet (no "[+] Screen on" output)
screen-toggle --help
Exit codes¶
| Code | Meaning |
|---|---|
0 |
screen is as requested (or already was) |
1 |
couldn't change |
2 |
no root / couldn't read state |
64 |
invalid usage |
How it acts¶
- reads the current state (same as
screen-state); - if already as you want, does nothing and exits
0; - sends
KEYCODE_WAKEUP(224) to turn on orKEYCODE_SLEEP(223) to turn off — both idempotent (they don't toggle like the power button); - waits up to 3 s confirming the change (re-read);
- if the device ignored the keycode, tries once the power button
(
KEYCODE_POWER, 26) as fallback; - if it still didn't change, exits
1with a clear message.
Why not the power button directly?¶
KEYCODE_POWER (26) only toggles — you don't know whether it'll turn on or
off without reading state first, and even reading it there can be a race
condition. KEYCODE_WAKEUP and KEYCODE_SLEEP are designed to be stable:
"wake" always wakes, "sleep" always sleeps, regardless of prior state. That's
why they're the first choice; power is only fallback.
Automation examples¶
# turn off at midnight, on at 6am
0 0 * * * screen-toggle --off -q
0 6 * * * screen-toggle --on -q
# only turn on if off (doesn't turn off if already on)
screen-toggle --on -q
# quiet in backup script: off during, on after
screen-toggle --off -q
rsync -av /data/ user@nas:/backup/
screen-toggle --on -q
Privacy¶
Neither reads, writes, or sends device data. Only display state and input commands.
See also¶
- Automation — full cron jobs with screen-toggle + battery.
- Troubleshooting — screen won't change state.
- Architecture — where
dumpsys powerandinput keyeventfit.