Android short screen brightness code!

前端 未结 3 1832
感动是毒
感动是毒 2021-01-03 04:27

Anyone who knows why this code ain\'t lowering the backlight of my application?

Context context = this;

    Settings.System.putInt(context.getContentResolve         


        
相关标签:
3条回答
  • 2021-01-03 04:48

    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);
    
    0 讨论(0)
  • 2021-01-03 04:59

    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();
    
    0 讨论(0)
  • 2021-01-03 04:59

    follow this code , i think i have put a comment on your previous question about that :)

    refer tis tuto

    0 讨论(0)
提交回复
热议问题