4

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?

Prizoff
  • 4,486
  • 4
  • 41
  • 69
DoctorAV
  • 1,189
  • 1
  • 14
  • 40

1 Answers1

9

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" />
Chirag
  • 56,621
  • 29
  • 151
  • 198