3

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!

dckrooney
  • 3,041
  • 3
  • 22
  • 28
  • Your question is identical to this one: http://stackoverflow.com/q/3737579/83446 Did you even run a search first? Somebody mark as duplicate. – Robert Massaioli Jun 01 '11 at 04:04
  • 6
    @Robert Massaioli: I don't think this is a duplicate because the question you linked to is about `changing screen brightness programmatically` while here the poster is asking about `how to make change to Android screen brightness persistent`. – Mudassir Jun 01 '11 at 05:48
  • @Robert Massaioli: Actually, I did perform a search. In fact, I read that exact question. However, as Mudassir pointed out, my issue is not simply adjusting brightness within the app; the problem lies in making those changes last even after the app has been paused or stopped. – dckrooney Jun 01 '11 at 07:16
  • 2
    Point taken. You are right they are different points. My mistake. – Robert Massaioli Jun 01 '11 at 12:18

4 Answers4

0

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

Akos Cz
  • 12,711
  • 1
  • 37
  • 32
0

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

sherpya
  • 4,890
  • 2
  • 34
  • 50
0

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);
Sanjeev
  • 4,255
  • 3
  • 28
  • 37
0

Here maybe help you:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);
Marco
  • 56,740
  • 14
  • 129
  • 152