Rosenpin Rosenpin - 1 year ago 197
Java Question

How to turn off the capacitive buttons light on Samsung devices programmatically?

I need to turn off the capacitive buttons light in my app.

I found this popular implementation

Settings.System.putInt(getContentResolver(), "button_key_light", 0);

Sadly, I get this exception on more recent Samsung devices

java.lang.IllegalArgumentException: You cannot keep your settings in the secure settings.

Is there a known way to implement something like this?

I have found this app and it seems to work fine with even the most recent devices, so I tried to decompile it and could only find the same line of code I mentioned above, which I really don't understand. Maybe the decompilation process changed a saved String in the system to "button_key_light"?

Maybe it's because of more recent build tools?

So does anyone have a known way of getting it to work?
Or at least an explanation for why my current code doesn't work?

Answer Source

OK I figured this out.

It seems like you have to target sdk version 22 or lower to write system settings that are not predefined.

So in order to run

Settings.System.putInt(getContentResolver(), "button_key_light", 0);

You have to add this to your app build.gradle

defaultConfig {
    targetSdkVersion 22