Change screen brightness onPause (Android app)
Asked Answered
P

1

1

My app saves the user original brightness with:

originalBrightness = Settings.System.SCREEN_BRIGHTNESS;

and then changes it while the program running.

originalBrightness is private global string. Now what I am trying to do is when the user clicks on "Home" or when the app paused the original screen is need to be set again, I have tried to do this like this but the app crushes:

public void onPause() {
    super.onPause();
    int brightness = Integer.getInteger(originalBrightness);
    setBrightness(brightness);
}
private void setBrightness(int brightness) {
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = brightness / 100.0f;
    getWindow().setAttributes(layoutParams);
}

Is there away to male it work? Thanks

Precarious answered 3/11, 2012 at 19:45 Comment(0)
C
1

Try Using Settings.System.SCREEN_BRIGHTNESS to set system default brightness as:

android.provider.Settings.System.putInt(getContentResolver(),  
android.provider.Settings.System.SCREEN_BRIGHTNESS,brightness /100.0f); // 0-255 

and add these permission's in manifest :

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Cantankerous answered 3/11, 2012 at 19:53 Comment(2)
can you explain what dose android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS,brightness /100.0f); // 0-255Precarious
@VladIoffe :currently you are setting brightness for Activity not for device default brightness as you mention in question when the user clicks on "Home" or when the app paused the original screen is need to be set again means you want to restore device default brightness then this is done automatically from device.if you have any issue then you will need to modify system default settings as my answerCabezon

© 2022 - 2024 — McMap. All rights reserved.