以编程方式更改 Android 上的显示亮度后更新它

发布于 2024-10-06 02:47:51 字数 426 浏览 6 评论 0原文

我正在尝试从小部件更新显示亮度,但遇到一些问题。

要更改亮度级别,我使用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话墙 2024-10-13 02:47:51

我遇到了类似的问题,只是创建了一个没有 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.

谈场末日恋爱 2024-10-13 02:47:51

首先,LayoutParams中要修改的值为 screenBrightness 。然后你必须执行 window.setAttributes 来应用它。正如 GeekYouUp 所说,您可以创建一个虚拟活动来获取您的 Window 对象。

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.

木緿 2024-10-13 02:47:51

您可以在 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,但您只需需要上下文。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文