Android Screen Timeout

Tom picture Tom · Jul 11, 2009 · Viewed 23.9k times · Source

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.

Answer

user788435 picture user788435 · Aug 24, 2010
public class HelloWorld extends Activity 
{
    private static final int DELAY = 3000;
    int defTimeOut = 0;

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

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

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);
    }
}

And also dont forget to add this permission in manifest:

android:name="android.permission.WRITE_SETTINGS"