[英]Android short screen brightness code!
谁知道为什么这段代码没有降低我的应用程序的背光?
Context context = this;
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 255);
不再允许应用程序修改全局亮度。 不要使用人们试图在各个方面提出的技巧,这些技巧使用私有 API,并且会在不同设备上以各种方式破坏(并且被认为是在平台的更新版本上已关闭的安全漏洞)。
官方 API 设置亮度是用 WindowManager.LayoutParams.screenBrightness,它可以让你为自己的应用程序的 window 设置亮度。 当用户进出您的应用程序时,该平台将自动更改亮度。
使用它来改变它:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = <some value between 0 and 1>;
getWindow().setAttributes(lp);
如果要更改当前应用程序的亮度,请使用发布的代码 hackbod
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = <some value between 0 and 1>;
getWindow().setAttributes(lp);
但我不能完全同意 hackbod 的帖子。 绝对有可能在不使用黑客的情况下改变全局亮度。 我刚刚写了一个简短的演示应用程序。
诀窍是,首先必须更改应用程序的亮度,然后更改全局亮度。 否则只有设置菜单中的“亮度滑块”会改变其 position 但这不会影响亮度。 只有当用户点击 slider 时,才会应用亮度。
WindowManager.LayoutParams localLayoutParams = getWindow()
.getAttributes();
localLayoutParams.screenBrightness = 0.12F;
getWindow().setAttributes(localLayoutParams);
Settings.System.putInt(this.resolver, "screen_brightness", 30);
应用程序亮度范围从 0 - 1 全局亮度范围从 0 - 255(0 = 显示关闭)
此外,如果您想在之后退出,等待一段时间以应用设置也非常重要。
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("finally exit");
finish();
}
});
t.start();
按照这段代码,我想我已经对你之前的问题发表了评论:)
参考 tis tuto
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.