How to get the current screen brightness in Android code dynamically?

How to get the current screen brightness in Android code dynamically?

+7
source share
4 answers

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(); } 
+13
source

You can read the contents of this system file

/ Sys / class / LEDs / LCD backlight / brightness

This value is the current screen brightness in the range 0-255

+4
source

This was asked some time ago, but simply in order to expand the dummy answer:

settings.screenBrightness will return -1 if it has not already been overwritten in code. This is the correct behavior, as setting the Brightness screen to -1 sets the brightness to the current system brightness level.

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

+1
source

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

you can change the brightness to

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

Also give an answer to Hamior for further explanation.

-one
source

All Articles