简体   繁体   English

以编程方式更改后,在Android上更新显示亮度

[英]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(); 我读到WindowManager.LayoutParams lp = getWindow().getAttributes(); should be used but I am working in a App Widget so getWindow() cannot be called. 应该使用,但我在App Widget中工作,因此无法调用getWindow()。

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

First, the value to modify in LayoutParams is screenBrightness . 首先,要在LayoutParams中修改的值是screenBrightness You'll then have to do a window.setAttributes to apply it. 然后,您必须执行window.setAttributes以应用它。 As GeekYouUp said, you can make a dummy activity to get your Window object. 正如GeekYouUp所说,你可以做一个虚拟活动来获取你的Window对象。

Can you use this code in your RemoteView, 您可以在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. 当您从用户定义的类中设置手机屏幕亮度时,此代码很有效,该类不会扩展活动,但您只需要上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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