Can't apply system screen brightness programmatically in Android

后端 未结 4 2055
闹比i
闹比i 2020-11-28 11:24

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

    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System         


        
4条回答
  •  感情败类
    2020-11-28 12:01

    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().

提交回复
热议问题