15

can I programmatically set screen to off in android if is not done automatically after 1 minute of inactivity.

is this possible to do programmatically in android?

I found this threads: Android: How to turn screen on and off programmatically?

Turning screen on and off programmatically not working on some devices

but there is no timer after 1 minute.

Community
  • 1
  • 1
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • Possible duplicate of [Android: How to turn screen on and off programmatically?](https://stackoverflow.com/questions/9561320/android-how-to-turn-screen-on-and-off-programmatically) – tir38 Jul 19 '19 at 18:16

1 Answers1

22

Yes, the method best used in this case is to programmatically set screen timeout instead.

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

1000 is in milliseconds which means 1 second, you can replace it with any value as desired.

Needed permission:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

UPDATE

It will overwrite the phone system value (Settings/Display/Sleep), so perhaps you need to restore the current settings after finish:

private static final int SCREEN_OFF_TIME_OUT = 13000;
private int mSystemScreenOffTimeOut;
private void setScreenOffTimeOut() {
    try {
        mSystemScreenOffTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, SCREEN_OFF_TIME_OUT);
    } catch (Exception e) {
        Utils.handleException(e);
    }
}

private void restoreScreenOffTimeOut() {
    if (mSystemScreenOffTimeOut == 0) return;
    try {
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, mSystemScreenOffTimeOut);
    } catch (Exception e) {
        Utils.handleException(e);
    }
}
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • from this way you are trying to set default system screen time out instead of only for your own application. – ρяσѕρєя K Dec 04 '12 at 06:31
  • Eventually, your app has to use the system inorder to set the screen off. An app on its own cannot get the screen to turn off. One minute of inactivity on your app is same as one minute of inactivity on the screen right? – Royston Pinto Dec 04 '12 at 06:33
  • if we use `Settings.System.SCREEN_OFF_TIMEOUT` for setting screen time out then setting is still renaming even if user moved from Application. – ρяσѕρєя K Dec 04 '12 at 06:36
  • 2
    Yes, thats how it will work. else another work around is for him, after scheduling a one minute handler, set screen timeout to 1 second, so that it turns off quickly. Then use a broadcast reciever to recieve ACTION_SCREEN_OFF and reset screen timeout to older value. – Royston Pinto Dec 04 '12 at 06:40
  • Is there any way to handle users' touches when we set the screen to off as above? – Mohamad Ghaith Alzin Oct 05 '22 at 05:24
  • I've used this to remove the time out `Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);` in which we set the timeout to set the screen off to the max value of the integer, as we say do not turn off the screen again! – Mohamad Ghaith Alzin Oct 05 '22 at 05:27