Java 将屏幕亮度控制添加到 android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1791340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:40:42  来源:igfitidea点击:

Adding screen brightness controls to android application

javaandroid

提问by Cymon

I am looking to add controls to adjust screen brightness locally in my app menu but can't seem to figure out how to do it. I have seen examples to max-out or dim brightness but I am looking to add controls so that the user can control and set the brightness level. Does anyone have any examples, tutorials, source code, or just a place to point me in the right direction?

我希望在我的应用程序菜单中添加控件以在本地调整屏幕亮度,但似乎无法弄清楚如何去做。我看过最大化或调暗亮度的示例,但我希望添加控件,以便用户可以控制和设置亮度级别。有没有人有任何例子、教程、源代码,或者只是一个地方来指出我正确的方向?

回答by Jeremy Logan

The internetclaims this works, I haven't tried it though:

互联网声称这有效,但我还没有尝试过:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

回答by Timothy Lee Russell

This is answered by a similar question.

这是由一个类似的问题回答的。

The only difference is that you will want to tie the screenBrightnessmember to the value of a user interface control.

唯一的区别是您需要将screenBrightness成员与用户界面控件的值联系起来。

Note that this might not work as expected on devices that have auto-dimming sensors.

请注意,这在具有自动调光传感器的设备上可能无法正常工作。

回答by mylock

You can change the user's brightness setting like this (make sure you declare permission for WRITE_SETTINGS in the manifest)

您可以像这样更改用户的亮度设置(确保在清单中声明对 WRITE_SETTINGS 的权限)

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

The documentation calls that setting from 0 to 255. I am trying to figure out whether it shuts the screen off at 0, as at one point I had a widget installed with a slider and you'd cause the screen to shut off if you set it to 0.

文档将该设置称为从 0 到 255。我试图弄清楚它是否会在 0 时关闭屏幕,因为有一次我安装了一个带有滑块的小部件,如果你设置,你会导致屏幕关闭它到 0。