29

just wondering whether it's possible to change the screen timeout using code in Android

enter image description here

kakopappa
  • 5,023
  • 5
  • 54
  • 73
  • Check this link [http://stackoverflow.com/questions/1114270/android-screen-timeout][1] [1]: http://stackoverflow.com/questions/1114270/android-screen-timeout – Raghu Nagaraju May 25 '12 at 05:31
  • Possible duplicate of [android-screen-timeout](http://stackoverflow.com/questions/1114270/android-screen-timeout) – Imran Rana May 25 '12 at 05:38

5 Answers5

52

It is simple to do.. You should learn to solve your problem from Android source code.

  /**
   * set screen off timeout
   * @param screenOffTimeout int 0~6
   */
private void setTimeout(int screenOffTimeout) {
    int time;
    switch (screenOffTimeout) {
    case 0:
        time = 15000;
        break;
    case 1:
        time = 30000;
        break;
    case 2:
        time = 60000;
        break;
    case 3:
        time = 120000;
        break;
    case 4:
        time = 600000;
        break;
    case 5:
        time = 1800000;
        break;
    default:
        time = -1;
    }
    android.provider.Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, time);
}
mhu
  • 17,720
  • 10
  • 62
  • 93
Klaudo
  • 680
  • 7
  • 7
  • 7
    if someone isnt working you may need the permissions – Javier Jan 15 '13 at 22:41
  • 1
    This does not have an effect on the lockscreen of 4.4+ devices. Anyone have a solution? – Flyview Jul 16 '14 at 22:53
  • 3
    Will this restore the previous value when my Activity is exited or killed? – Michael Feb 14 '16 at 16:35
  • Is it possible to set it less than 15 seconds? I tried in 7.1 but did work for 1 second. – shantanu Jul 01 '17 at 15:16
  • Hi. I wish to know if there is a way increment the time on screen off whenever there is some process going on. In my case i want the timeout to extend when there is a Data Upload process going over bluetooth. – Kishan Aug 03 '18 at 06:03
14

A better solution is to do one of the following (depending on whether you want it to be dynamic or static):

  1. Specify attribute android:keepScreenOn in layout (xml) (i.e. indefinitely prevent screen timeout at all times),
  2. Add the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag when you create your Activity, or
  3. Use a WakeLock to control how long the screen should be up (dynamic)
Andrew
  • 75
  • 7
Thira
  • 1,555
  • 1
  • 13
  • 24
8

Setting the screen timeout to -1 does not seem to do an accurate job of what is required.

I've found that setting the value to Integer.MAX_VALUE works better.

For example:

android.provider.Settings.System.putInt(content, Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);

This seems to set the max timeout to the maximum allow by the device.

For example, if when navigating to the display settings on your phone only allows you to have a maximum screen timeout of 30 minutes, doing the above code will set the screen timeout to 30 minutes.

TheBandi
  • 81
  • 1
  • 1
  • 1
    The last parameter takes a string, not an int, should be String.valueOf(Integer.MAX_VALUE) – keag Aug 16 '17 at 18:53
  • 1
    This is exactly answer for set Never is put Integer.MAX_VALUE! It is what OS set in the ContentProvider when you choose Never. – Taras Okunev Feb 19 '19 at 10:49
5

If anybody needs to set it to never, here is the code

Settings.System.putString(cr, Settings.System.SCREEN_OFF_TIMEOUT, "-1");
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
kakopappa
  • 5,023
  • 5
  • 54
  • 73
0

Try this. Just change 3000 to whatever screen out time you want. Make sure you provide change system settings permission.

try {
    int mSystemScreenOffTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 3000);
} catch (Exception e) {
    Toast.makeText(this, "Permission Error", Toast.LENGTH_SHORT).show();
}
fcdt
  • 2,371
  • 5
  • 14
  • 26