简体   繁体   中英

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).

The official API to set the brightness is with WindowManager.LayoutParams.screenBrightness, which lets you set the brightness for your own app's 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

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. 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. Only if the user taps on the slider, the brightness will be applied.

    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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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