Search code examples
androidbrightness

How to retrieve current system Screen brightness value in android?


I am trying to create an app which will set brightness of an android device programmatically. I have manage to change device brightness using the following code:

BackLightValue = (float)arg1/100;
BackLightSetting.setText(String.valueOf(BackLightValue));

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);

I have retrieve current system brightness value using following code:

try {
    BackLightValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch(Exception e) {}

Above code should give me current set value for brightness but when I start an app it shows default value at 50%.

What is going wrong?


Solution

  • This is correct behaviour as setting screenBrightness to -1 will set the brightness to the current system brightness level.

    int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);
    

    Permission in Manifest file.

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />