Android - Turn off screen without going in StandBy Mode

前端 未结 2 520
星月不相逢
星月不相逢 2020-12-13 22:04

I know that this question has been asked a lot of times but it has never been answered satisfactorily.

My problem is the following:

I have an activity which

2条回答
  •  醉梦人生
    2020-12-13 22:44

    Seems like it isn't possible to turn off the screen AND reactivate just by touching the display.

    My new approach now:

    private WakeLock screenWakeLock;
    
    PowerManager pm = PowerManager.getSystemService(Context.POWER_SERVICE);
    screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                    "screenWakeLock");
    screenWakeLock.acquire();
    

    The PARTIAL_WAKE_LOCK keeps the CPU running but allows the display to shut down.

    When power or home button is pressed the display turns on again and the activity becomes visible again (without having to "slide-to-unlock" or sth. else).

    Don't forget to release the screenWakeLock.

    In my case I did it in the onResume() of the activity:

    if (screenWakeLock != null) {
       if(screenWakeLock.isHeld())
          screenWakeLock.release();
       screenWakeLock = null;
    }
    

    Maybe this helps someone with a similar problem in the future.

提交回复
热议问题