Android: How to turn screen on and off programmatically?

后端 未结 16 2056
醉酒成梦
醉酒成梦 2020-11-22 13:05

Before marking this post as a \"duplicate\", I am writing this post because no other post holds the solution to the problem.

I am trying to turn off the device, then

16条回答
  •  伪装坚强ぢ
    2020-11-22 13:23

    Here is a successful example of an implementation of the same thing, on a device which supported lower screen brightness values (I tested on an Allwinner Chinese 7" tablet running API15).

    WindowManager.LayoutParams params = this.getWindow().getAttributes();
    
    /** Turn off: */
    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    //TODO Store original brightness value
    params.screenBrightness = 0.1f;
    this.getWindow().setAttributes(params);
    
    /** Turn on: */
    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    //TODO restoring from original value
    params.screenBrightness = 0.9f;
    this.getWindow().setAttributes(params);
    

    If someone else tries this out, pls comment below if it worked/didn't work and the device, Android API.

提交回复
热议问题