7

How to get current screen's brightness in Android code dynamiclly?

yuankai
  • 167
  • 1
  • 4
  • 13

4 Answers4

14

Hi to get the current brightness level of the android system you can use this code:

try {
    float curBrightnessValue=android.provider.Settings.System.getInt(
    getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
r_ahlskog
  • 1,916
  • 1
  • 17
  • 26
Midhun VP
  • 629
  • 1
  • 10
  • 18
  • 5
    Nope, it reads current settings of brightness, not the current brightness. – greenoldman Aug 02 '12 at 14:56
  • 2
    @greenoldman That is true. Current brightness can be retrieved by `getWindow().getAttributes().screenBrightness`. If however this current brightness is < 0 this means that application uses the current settings for brightness (which can be loaded by the means provided in current answer) – Alex Semeniuk Dec 16 '13 at 10:18
  • `getInt()` Method return Int value.But Brightness store as a float.Better to use `getFloat()` – Elshan May 20 '14 at 04:33
  • @Jimmer Nope, as it returns `The screen backlight brightness between 0 and 255.` – Muhammed Refaat Sep 30 '14 at 10:25
  • @MuhammedRefaat, Android can change brightness automatically (hint: dim). – greenoldman Sep 30 '14 at 12:11
  • @greenoldman So, we have here to set the settings of `SCREEN_BRIGHTNESS_MODE_MANUAL` to avoid that, right ? – Muhammed Refaat Sep 30 '14 at 12:27
5

This was asked a while ago but just to expand on fiction's answer:

settings.screenBrightness will return -1 if it has not been previously overwritten in code. This is correct behaviour as setting screenBrightness to -1 will set the brightness to the current system brightness level.

This system brightness can be changed by the user at any time, so there is probably not much use in storing the actual value, if you are just trying to return the brightness to its original value, as the actual value might be "out of date".

Hamiora
  • 561
  • 5
  • 5
4

You can read content of this system file

/sys/class/leds/lcd-backlight/brightness

This value is current screen brightness within range 0-255

-1

Acording to this - http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

you can change brightness by

WindowManager.LayoutParams settings = getWindow().getAttributes();
settings.screenBrightness = newValue;
getWindow().setAttributes(settings);

Also refer Hamiora answer for further explanation.

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • 3
    hi, float savedBrightness = settings.screenBrightness; cannot get the current brightness but whether is overrided by app, and i try it but always get -1.0f from it, because I do not override it! – yuankai Dec 29 '10 at 02:17
  • @yuankai You should perform additional check. If your `savedBrightness` < 0 that means that application uses the default brightness. The default brightness level can be retrieved by MidhunVP's answer. (just keep in mind that `settings.screenBrightness` ranges from 0f to 1f while system setting has values from 0 to 255) – Alex Semeniuk Dec 16 '13 at 10:21