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?
2 Answers
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.

- 2,336
- 5
- 31
- 40
-
Some images mount `sys` in `/sys`, which would render the path to `/sys/class/leds/button-backlight/brightness` - that works on Elephone P5000 for example. – Ján Sáreník Aug 07 '15 at 06:28
-
1-- oops, now I noticed the path is mentioned in the quoted part of answer. Sorry for duplicity. – Ján Sáreník Aug 07 '15 at 06:48
This is applicable only for the device samsung devices:
To get the BackLight sate:
int backLight = Settings.System.getInt(getContentResolver(), "button_key_light");
// if it return -1 it means that light is on
// if it return 0 the light is off
// some time it will return values like 600(1.5 sec)
if you want to put the backLight as off u can do like this
Settings.System.putInt(getApplicationContext().getContentResolver(), "button_key_light", 0);

- 808
- 9
- 18
-
I'm getting this exception when trying putInt java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings. – Rosenpin Jun 20 '16 at 23:16
-
in the Samsung device which has backLight future , it has system variable as "button_key_light" -1 for "on" 0 for "off" – KomalG Jun 21 '16 at 10:47
-
I got that, but your code for setting them cause an exception and doesn't work, at least on the device I tested it on (Galaxy S7) – Rosenpin Jun 21 '16 at 13:46
-
1