以编程方式更改屏幕亮度(与电源小部件一样)
我正在搜索如何以编程方式更改屏幕的亮度,我发现这个这是非常好的解决方案,而且效果很好,但它仅在我的应用程序处于活动状态时才有效。我的应用程序关闭后,亮度将返回与启动应用程序之前相同的值。
我希望能够更改亮度,就像我按下电源小部件中的亮度按钮一样。在来自 android 的电源小部件中,有 3 种状态。一种非常亮,一种非常暗,一种介于两者之间。是否可以像有人按下此小部件一样改变亮度?
编辑1: 我创建了这个,并向我的清单添加了权限,但是当应用程序启动时,我没有看到亮度有任何变化,我尝试使用 10 和 100,现在使用 200,但没有任何变化 有什么建议吗?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}
I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. After my application is shutdown then the brightness is returned back the the same value as before I start my app.
I want to be able to change the brightness just like when I press on the brightness button from my power widget. In the power widget that comes from android there are 3 states. One very bright one very dark and one in between. Is it possible to change the brightness just like when someone press on this widget ?
Edit1:
I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes
any suggestions ?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
android.provider.Settings.System.putInt(this.getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, 200);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我发现有效的完整代码:
我的问题中的代码不起作用,因为屏幕没有刷新。因此,刷新屏幕的一种技巧是启动虚拟活动,然后在创建该虚拟活动时调用finish(),以便亮度的更改生效。
This is the complete code I found to be working:
The code from my question does not work because the screen doesn't get refreshed. So one hack on refreshing the screen is starting dummy activity and than in on create of that dummy activity to call
finish()
so the changes of the brightness take effect.使用该链接中的 Tor-Morten 解决方案,同时设置系统亮度设置,如下所示:
其中
bright
是一个范围从 1 到 255 的整数。Use Tor-Morten's solution in that link, but also set the system brightness setting like so:
Where
bright
is an integer ranging from 1 to 255.我今天已经解决了这个问题。
首先,您必须将权限放入 AndroidManifest.xml 文件中:
将其放入文件中的确切位置在哪里?
此权限表示您可以更改也影响其他应用程序的设置。
现在您可以设置亮度自动模式打开和关闭
自动模式现在打开还是关闭? 因此,如果您
想手动更改亮度,您应该先设置手动模式,然后才能更改亮度。
注意:SCREEN_BRIGHTNESS_MODE_AUTOMATIC 为 1
注意:SCREEN_BRIGHTNESS_MODE_MANUAL 为 0
您应该使用此值
而不是此值
现在您可以手动更改亮度
并读取亮度
现在一切都已正确设置,但是...您还看不到更改。
您还需要一件事才能看到变化!
刷新屏幕...所以这样做:
不要忘记权限...
I have solved this problem today.
As the first you have to put a permission into your AndroidManifest.xml file:
Where is the exact place to put it in the file?
This permission says, that you are allowed to change settings that affect other applications too.
Now you can set brightness automatic mode on and off
Is the automatic mode turned on or off right now? You can get the information
So, if you want to change brightness manually, you are supposed to set the manual mode first and after that you can change the brightness.
note: SCREEN_BRIGHTNESS_MODE_AUTOMATIC is 1
note: SCREEN_BRIGHTNESS_MODE_MANUAL is 0
You should use this
instead of this
Now you can change the brightness manually
and read brightness
Now everything is set properly, but... you can't see the change yet.
You need one more thing to see the change!
Refresh the screen... so do this:
Don't forget the permission...
在设置参数时传递活动的上下文也可以完成工作,而无需启动另一个活动。
以下内容对我有用 -
我在 onSensorChanged() 方法中添加了这段代码,每当触发时,它就会使显示变暗。
Passing the context of the activity while setting up the parameters would also get the job done without any need to start another activity.
The following worked for me-
i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered.
复杂的例子:
COMPLEX EXAMPLE: