15

I'm using the following to set the system auto brightness mode and level:

    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1);

I can change auto-brighess on and off, and set different levels. The settings seem to be applied properly -- I can go to into Settings --> Display --> Brightness, and whanever setting I set is actually shown correctly. However, the actual screen isn't changing its brightness. If i just tap on the slider in Display Settings, then everything gets applied.

I shoudl mention that I'm running an app withat a main activity, and these settings are getting applied in the BroadcastReceiver. I did try to create a dummy activity and tested the stuff there, but got the same results.

Murphy
  • 4,858
  • 2
  • 24
  • 31
user496854
  • 6,461
  • 10
  • 47
  • 84

4 Answers4

17

OK, found the answer here: Refreshing the display from a widget?

Basically, have to make a transparent activity that processes the brightness change. What's not mentioned in the post is that you have to do:

Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0);
Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessLevel); 

then do

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

And if you call finish() right after applying the changes, brightness will never actually change because the layout has to be created before the brightness settings is applied. So I ended up creating a thread that had a 300ms delay, then called finish().

Community
  • 1
  • 1
user496854
  • 6,461
  • 10
  • 47
  • 84
  • Pity this method is not rock-solid reliable (flaw of the Android). Because of all timings you can find corner cases when it fails -- for example when locking (here I set new brightness) and unlocking phone. If there is a pause between those 2 events it works, but if actions are next to each other, changing brightness will be ignored. – greenoldman Oct 20 '12 at 19:59
  • it permanently sets the brightness to 0 or whatever and have to change the manually from settings then, – Saqib Jan 17 '14 at 07:25
2

Use the answer given by "user496854" above
If you are taking max screenBrightness =255 then while doing

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

divide screenBrightness by 255 like

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness/(float)255;   
getWindow().setAttributes(lp);
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
2

I'm doing something similar with screen brightness in one of my apps, and I'm doing it through the WindowManager and it works. I'm using the following code to get the current screen brightness (and save it for later) and set it to full:

    WindowManager.LayoutParams lp = getWindow().getAttributes();
    previousScreenBrightness = lp.screenBrightness;
    float brightness = 1;
    lp.screenBrightness = brightness; 
    getWindow().setAttributes(lp); 
Rich
  • 36,270
  • 31
  • 115
  • 154
  • 2
    thanks, and this is definitely an improvement, but it really doesn't work properly -- it only works if the dummy activity is running, and it has to open it to change the bightness. I tried having the activity finish immediately after the change, but then the brightness doesn't get changed. – user496854 Feb 17 '11 at 18:30
  • 4
    Also, I just realized that this controls the brightness of only the current activity. Once it's closed, the brightness resets to the default system setting. So we're back to the original question -- how can I ge the brightness system setting to get applied? – user496854 Feb 20 '11 at 03:20
  • Awesome! this was exactly what I needed. I was setting the screenbrightness directly and it wasn't being updated. I actually needed to set the window attributes to force it to update. Cheers! – Pellet Mar 09 '18 at 02:18
1

I created a static method in my Application class which I invoke from all my Activity.onResume() methods.

MyApplication extends Application {
    ...
    public static void setBrightness(final Activity context) {
        // get the content resolver
        final ContentResolver cResolver = context.getContentResolver();
        // get the current window
        final Window window = context.getWindow();

        try {
            // get the current system brightness
            int brightnessLevel = System.getInt(cResolver,System.SCREEN_BRIGHTNESS);
            // get the current window attributes
            LayoutParams layoutpars = window.getAttributes();
            // set the brightness of this window
            layoutpars.screenBrightness = brightnessLevel / (float) 255;
            // apply attribute changes to this window
            window.setAttributes(layoutpars);
        } catch (SettingNotFoundException e) {
            // throw an error cuz System.SCREEN_BRIGHTNESS couldn't be retrieved
            Log.e("Error", "Cannot access system brightness");
            e.printStackTrace();
        }
    }
}

MyActivity extends Activity {
    ...
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()");
        MyApplication.setBrightness(this);
    }
}
Akos Cz
  • 12,711
  • 1
  • 37
  • 32