Android - Turn off screen without going in StandBy Mode

前端 未结 2 535
星月不相逢
星月不相逢 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();
    }
    

提交回复
热议问题