Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 347
DoctorAV Avatar asked Oct 24 '25 17:10

DoctorAV


1 Answers

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" />
like image 133
Chirag Avatar answered Oct 26 '25 06:10

Chirag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!