Android: How to turn screen on and off programmatically?

后端 未结 16 2411
醉酒成梦
醉酒成梦 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:37

    I would suggest this one:

    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
    wl.acquire();
    

    The flag ACQUIRE_CAUSES_WAKEUP is explained like that:

    Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

    Also, make sure you have the following permission in the AndroidManifewst.xml file:

    
    

提交回复
热议问题