简体   繁体   English

Android短屏亮度码!

[英]Android short screen brightness code!

Anyone who knows why this code ain't lowering the backlight of my application?谁知道为什么这段代码没有降低我的应用程序的背光?

Context context = this;

    Settings.System.putInt(context.getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS, 255);

Applications are no longer allowed to modify the global brightness.不再允许应用程序修改全局亮度。 Don't use the tricks people have tried to come up with at various points, these use private APIs and will break in various ways across different devices (and are considered security holes that have been closed on more recent versions of the platform).不要使用人们试图在各个方面提出的技巧,这些技巧使用私有 API,并且会在不同设备上以各种方式破坏(并且被认为是在平台的更新版本上已关闭的安全漏洞)。

The official API to set the brightness is with WindowManager.LayoutParams.screenBrightness, which lets you set the brightness for your own app's window.官方 API 设置亮度是用 WindowManager.LayoutParams.screenBrightness,它可以让你为自己的应用程序的 window 设置亮度。 The platform will automatically take care of changing the brightness as the user moves in and out of your app.当用户进出您的应用程序时,该平台将自动更改亮度。

Use this to change it:使用它来改变它:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = <some value between 0 and 1>;
getWindow().setAttributes(lp);

If you want to change the brightness of your current application, use the code hackbod posted如果要更改当前应用程序的亮度,请使用发布的代码 hackbod

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = <some value between 0 and 1>;
getWindow().setAttributes(lp);

But I cannot fully agree with hackbod's post.但我不能完全同意 hackbod 的帖子。 It is definitely possible to change the global brightness without using hacks.绝对有可能在不使用黑客的情况下改变全局亮度。 I just wrote a short demo application.我刚刚写了一个简短的演示应用程序。

The trick is, that first the application's brightness has to be changed and then change the global brightness.诀窍是,首先必须更改应用程序的亮度,然后更改全局亮度。 Otherwise only the "brightness slider" in the settings menu changes its position but this does not affect the brightness.否则只有设置菜单中的“亮度滑块”会改变其 position 但这不会影响亮度。 Only if the user taps on the slider, the brightness will be applied.只有当用户点击 slider 时,才会应用亮度。

    WindowManager.LayoutParams localLayoutParams = getWindow()
            .getAttributes();
    localLayoutParams.screenBrightness = 0.12F;
    getWindow().setAttributes(localLayoutParams);

    Settings.System.putInt(this.resolver, "screen_brightness", 30);

application brightness range from 0 - 1 global brightness range from 0 - 255 (0 = display off)应用程序亮度范围从 0 - 1 全局亮度范围从 0 - 255(0 = 显示关闭)

Also it is very important to wait some time to apply the settings if you want to exit afterwards.此外,如果您想在之后退出,等待一段时间以应用设置也非常重要。

    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();

follow this code, i think i have put a comment on your previous question about that:)按照这段代码,我想我已经对你之前的问题发表了评论:)

refer tis tuto参考 tis tuto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM