5

Is there any way to quickly toggle the screen off without making Android sleep?

For example, I sometimes like to use the Calm app when I sleep. But it keeps the screen on all the time. When I just use that app for 10 minutes, it's great that it keeps the screen on; but if I'm sleeping for hours, I would like the app to keep on running (playing sounds) and turn the screen off.

How can this be done?

End Anti-Semitic Hate
  • 4,400
  • 23
  • 61
  • 100
  • 3
    For this the app needs to explicitly create a so-called "Wakelock" that prevents Android to set the CPU into Deep Sleep and keeps the whole system (including your app) running. – GiantTree Aug 19 '15 at 20:12
  • @GiantTree Right. Is there a way to quickly toggle that on/off for all apps or a specific app? – End Anti-Semitic Hate Aug 19 '15 at 21:04
  • AFAIK an app has to ask for such a wakelock on its own. Otherwise Android may just kill an app if it is running in the background without a lock. – GiantTree Aug 19 '15 at 21:52
  • I probably didn't understand the question. Can you not press the power button and let the screen turn off? If yes, does your app stops working? If true, this answer may be of some service: Is there a way to force an app to remain running in the background no matter what? – Firelord Aug 20 '15 at 02:17
  • @Firelord If you run the Calm app and don't press the power button, the screen will never turn off. After hours of sleep, I was happy that the screen image was not burned in. – End Anti-Semitic Hate Aug 20 '15 at 03:25
  • 1
    Changing /sys/class/leds/lcd-backlight/brightness to 0 does the job in my device. It doesn't put the device to sleep, but simply turn off the LEDs. Tested on that Calm app. You can hear its music yet not burning up the battery on screen. Requires root access though. – Firelord Aug 20 '15 at 10:02
  • @Firelord If it turns off the screen, how do you change the value back (since you can't see anything)? – End Anti-Semitic Hate Aug 20 '15 at 10:51
  • 1
    Press the power button two times with gap of one second, the screen would come back to normal. I tried it over ADB so I could always change the value. You may bind this behavior to a button using Xposed Additions. Or perhaps a time based event in Tasker could also be an option. – Firelord Aug 20 '15 at 10:53
  • @Firelord Thanks... I'll give it a try to see how it works. Is there a way to edit the file automatically? Going in with a root editor and changing the contents of the file each time is a bit laborious. – End Anti-Semitic Hate Aug 20 '15 at 11:03
  • Automation app like Tasker can do it. Task would be + -> File -> Write file -> your content. Or, + -> Code -> Run Shell -> and write the file using command. – Firelord Aug 20 '15 at 11:16

2 Answers2

7

Credit goes to Firelord for the basic method. Here is a time-delay shell script that turns off the backlight for my device, and apparently others' as well.

Create a file named "lcdoff" and copy the following into it:

#!/system/bin/sh
if [ -z "$1" ]; then
    echo "usage: `basename $0` delay"
    exit
fi
su -c 'echo getsu > /dev/null'
echo "Tap power button 2x quickly to restore"
sleep $1
su -c 'echo 0 > /sys/class/leds/lcd-backlight/brightness'

Make sure:

  • The device is rooted.
  • The script permissions are set to executable, e.g. by chmod 755 lcdoff.
  • The script is located in /system/bin (scripts cannot be executed from /storage).

Then open a terminal app and enter lcdoff 10, to turn off the screen after a 10-second delay, lcdoff 30 for a 30-second delay, etc. To ensure that the processor stays on, simply enable the Android developer option "Stay Awake". Edit: Stay Awake only works while plugged into a power source -- see this answer for an alternative.

If you don't want to type the command in a terminal, you can launch the script as a custom action in:

  • Total Commander
  • Tasker
  • Xposed Additions
  • your app of choice
  • 1
    To avoid bumping my answer again, I'll add here the standby script I use: if [ "$1" == "0" ]; then su -c 'echo sbylock >/sys/power/wake_unlock'; elif [ "$1" == "1" ]; then su -c 'echo sbylock >/sys/power/wake_lock'; lcdoff 0; else echo "usage: $(basename $0) 0|1"; fi –  Mar 21 '16 at 14:30
3

I had some trouble with the accepted answer as I'm not hugely familiar with shell scripting on Android/Unix/Linux so I thought I'd add my experience here for anyone in a similar boat.

To get this working with Tasker or similar the line you need is:

echo 0 > /sys/class/leds/lcd-backlight/brightness

For tasker put this in the 'command' field with 'Use Root' enabled, works like a charm. To disable simply switch the zero out for a 1, ergo:

echo 1 > /sys/class/leds/lcd-backlight/brightness

or lock and unlock the device.

So to answer your question about quickly toggling this behavior. Assign each of the above commands to a tasker task, then you can make use of the new long-press intercept in the latest tasker version to toggle the screen on and off without putting the device to sleep. Alternatively you can assign the command to a profile which triggers when your conditions are met.

Helios210
  • 31
  • 1