Java – Android Screen Timeout


I know its possible to use a wakelock to hold the screen, cpu, ect on but how can I programmatically change the "Screen Timeout" setting on an Android phone.

Best Solution

public class HelloWorld extends Activity 
    private static final int DELAY = 3000;
    int defTimeOut = 0;

    protected void onCreate(Bundle savedInstanceState) 
        // Be sure to call the super class.

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        defTimeOut = Settings.System.getInt(getContentResolver(), 
                         Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, DELAY);

    protected void onDestroy() 
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);

And also dont forget to add this permission in manifest: