在android中以编程方式改变屏幕亮度
我想在 android 中以编程方式更改屏幕亮度。 目前我使用这个代码:
WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
但是这个示例代码适用于纸杯蛋糕,而不适用于最新版本。我正在使用最新版本的 SDK。较新 Android 版本的首选解决方案是什么?
I want to change the screen brightness programmatically in android.
At the moment I use this code:
WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
But this sample code works on cupcake, not on latest versions. I am using the latest version of SDK.. What is the preferred solution for newer Android Versions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这可以通过使用来完成:
另请参阅:
http://developer.android.com/reference/android/view /WindowManager.LayoutParams.html#screenBrightness
This is possible to do by using:
See also:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness
在创建
Window
之前,您必须向它添加参数,否则会抛出java.lang.IllegalArgumentException:添加窗口后无法更改窗口类型。
请参阅示例使用
android.app.Dialog.Dialog
。请注意,亮度值介于 0.0F 和 1.0F 之间。
You have to add params to
Window
before it is created otherwise it will throwjava.lang.IllegalArgumentException: Window type can not be changed after the window is added.
See the example with a
android.app.Dialog.Dialog
.Please note that brightness value is between 0.0F and 1.0F.
为此使用 IHardwareService 接口怎么样?可以在此 教程。
更新:教程链接仍然有效,但实际代码也可以在下一个答案中找到。
How about using the IHardwareService interface for this? An example can be found in this tutorial.
Update: tutorial link still works, but actual code is also available in next answer.
答案太晚了,但想要改进..
我尝试使用 Tor-morten 的代码,但它是针对特定屏幕本身的,我想在任何地方进行更改,我为此提供了服务。
在android中根据周围的光线改变亮度
希望,它会对别人有用。
Too late answer but want to improve..
I tried with Tor-morten's code but it is for particular screen itself, I wanted to change anywhere, I made service for that.
Change brightness according to surrounding light in android
Hope, It will be useful to others.