简体   繁体   中英

How can I make changes to android screen brightness persistent?

I've managed to change screen brightness within an activity (and by extension the entire app) using the following code:

Settings.System.putFloat(this.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, ((float)LocationService.settings.screenBrightness));

WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness = someValue;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);

However, as soon as I close the app the brightness returns to its previous settings. Is there anyway to make these changes persist outside of the lifecycle of the app?

Thanks!

Try this

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

int brightnessLevel = 255;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessLevel);

You will need the WRITE_SETTINGS permission set in your AndroidManifest.xml

http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html

Settings application does it this way:

private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;

System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value + MINIMUM_BACKLIGHT);

but I suspect you cannot change a System Settings without having system shared user id

Provide the permission in androidMainfest.xml

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

And provide a integer number between 0 to 255

//number>=0 && number<=255
//If you have passed context then you can use context.getContentResolver()
android.provider.Settings.System.putInt(cgetContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS,
            number);

Here maybe help you:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM