Android - Turn off screen without going in StandBy Mode

前端 未结 2 521
星月不相逢
星月不相逢 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:25

    Note : i wasn't able to work with WAKELOCK

    I have figured a workaround which involves changing SCREEN_OFF_TIMEOUT. Use the below code to achieve it.

    Settings.System.putInt(getContentResolver(), 
                               Settings.System.SCREEN_OFF_TIMEOUT, 10);
    

    this sets 10 milliseconds as timeout for screen_timeout SYSTEM-WIDE .

    Now you might be troubled by SYSTEM-WIDE changes brought upon by this. To work around that you can get the default screen_timeout_time and save it to a variable then set back the System's SCREEN_OFF_TIMEOUT at finish() of your activity.

    Before setting 10ms as our screen_timeout get SCREEN_OFF_TIMEOUT,

    int defaultScreenTimeout= android.provider.Settings.
          System.getInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,-1);
    

    Now when you have finished with your changes or when your activity ends you may set the SCREEN_OFF_TIMEOUT back .

    @Override
    public void finish(){
        Settings.System.putInt(getContentResolver(), 
                          Settings.System.SCREEN_OFF_TIMEOUT, defaultScreenTimeout);
    super.finish();
    }
    
    0 讨论(0)
  • 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.

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