以编程方式更改 Android 上的显示亮度后更新它
我正在尝试从小部件更新显示亮度,但遇到一些问题。
要更改亮度级别,我使用:
Settings.System.putInt(context.getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
这会修改显示设置(实际上在“显示”->“亮度”中,级别是正确的),但显示的有效亮度不会更改。如果我锁定屏幕并解锁,亮度最终会更改为我设置的值。
我认为这是一个设置更新问题,那么如何在设置更改后立即更新显示设置?
我读到应该使用 WindowManager.LayoutParams lp = getWindow().getAttributes(); 但我正在应用程序小部件中工作,因此无法调用 getWindow() 。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
首先,LayoutParams中要修改的值为 screenBrightness 。然后你必须执行 window.setAttributes 来应用它。正如 GeekYouUp 所说,您可以创建一个虚拟活动来获取您的 Window 对象。
您可以在 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);
当您从用户定义的类内部设置手机屏幕亮度时,此代码可以正常工作,该类不扩展 Activity,但您只需需要上下文。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我遇到了类似的问题,只是创建了一个没有 UI 的活动来进行亮度更改,使用意图从应用程序小部件运行它。
I had a similar issue and just created an Activity with no UI to do the brightness change, used an intent to run it from the App Widget.