How to get current screen's brightness in Android code dynamiclly?
How to get current screen's brightness in Android code dynamiclly?开发者_JS百科
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();
}
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".
You can read content of this system file
/sys/class/leds/lcd-backlight/brightness
This value is current screen brightness within range 0-255
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.
精彩评论