Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change screen timeout programmatically?

Tags:

android

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

enter image description here

like image 503
kakopappa Avatar asked May 25 '12 05:05

kakopappa


3 Answers

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); } 
like image 127
Klaudo Avatar answered Sep 18 '22 08:09

Klaudo


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)
like image 44
Thira Avatar answered Sep 18 '22 08:09

Thira


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.

like image 45
TheBandi Avatar answered Sep 18 '22 08:09

TheBandi