Android - turn off hardware key lights

John picture John · Nov 11, 2010 · Viewed 15.9k times · Source

Inside my app, I need a way to turn off the lights on the standard Android phone keys (Home, Menu, Back, and Search) - how can I do this programmatically?

Answer

Jason Plank picture Jason Plank · Jan 27, 2011

According to this page, the hardware key backlights can be controlled by writing to a specific file in the filesystem with superuser privileges (i.e. phone must be "rooted"):

Q: How can I control the keyboard backlight?

A: The keyboard backlight can be controlled via /sys/class/leds/keyboard-backlight/brightness. It appears that it's a simple on-off control (echoing '0' turns it off, echoing '1' or higher turns it on). For some reason, the default system backlight control stuff seems to set this to "83", but I don't know why. I can't seem to see any difference between 83 and any other number. The file is readable by anyone, but only writable by root, so you'll need root access to the phone to manipulate it this way.

So to turn off the backlight programmatically, you could invoke exec() on the Runtime like so:

Runtime r = Runtime.getRuntime();
r.exec("echo 0 > /system/class/leds/keyboard-backlight/brightness");

Depends on what you are doing, but would probably be wise to check the result of exec() afterwards to see if a write error occurred.

Note: I tested this on my own phone and it seems to work without acting as root. However, this may not be the case on every phone, so you may have different results.