Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue on Setting Brightness on some Android device

I'm having trouble trying to set screen brightness. To do so I use the following code:

Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness); 

where brightness is a value between 0 and 255.

The problem is that in some device it works (Htc One, Nexus 5,...), in others dont! For example, if I use this function to set screen brightness in a Samsung device the screen doesnt get brighter or dimmer, altough if i go into "Notification Panel" (sliding down from the top of the screen) i can see the brightness level has changed.

Does anyone know a way to set brightness that works with all Android devices? And does anyone know why it works in some devices, but in other dont?

UPDATE 1:

This method is used in a BroadcastReceiver, so no Activities are there!

like image 227
Stefano Munarini Avatar asked Nov 21 '13 13:11

Stefano Munarini


1 Answers

You can override the brightness for the current window, instead of changing the brightness settings.

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    if (overrideBrightness)
        lp.screenBrightness =  BRIGHTNESS_OVERRIDE_FULL;
    else 
        lp.screenBrightness =  BRIGHTNESS_OVERRIDE_NONE;
    getWindow().setAttributes(lp);
like image 127
dangVarmit Avatar answered Oct 06 '22 20:10

dangVarmit