简体   繁体   中英

Update Display Brightness on Android after changing it programmatically

I'm trying to update the display brightness from a widget but i have some problems.

To change brightness level, i use:

Settings.System.putInt(context.getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);

This modifies the display setting (in fact in Display->Brightness the level is correct) but the effective brightness of display is not changed. If i lock the screen and unlock, the brightness finally changes to the value i set.

I assume this is a Settings Update issue, so how can the display settings be immediatly updated after settings change?

I read that WindowManager.LayoutParams lp = getWindow().getAttributes(); should be used but I am working in a App Widget so getWindow() cannot be called.

我有一个类似的问题,只是创建了一个没有UI的Activity来进行亮度变化,使用了一个意图从App Widget运行它。

First, the value to modify in LayoutParams is screenBrightness . You'll then have to do a window.setAttributes to apply it. As GeekYouUp said, you can make a dummy activity to get your Window object.

Can you use this code in your RemoteView,

Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);

// This makes the new screen brightness effective
WindowManager.LayoutParams layoutParams = ((Activity)context).getWindow().getAttributes();
float b = brightness/255.0f;
if(b == 0.0)    
    b = 0.01f;
layoutParams.screenBrightness = b;
((Activity)context).getWindow().setAttributes(layoutParams);

This code fine works when you are setting phone screen brightness from inside a User-defined class which is not extending an Activity but you only need the context.

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