Android Screen Timeout
Asked Answered
K

4

11

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.

Kattegat answered 11/7, 2009 at 17:47 Comment(0)
E
12

The Settings.System provider offers a SCREEN_OFF_TIMEOUT setting that might be what you are looking for.

Eusporangiate answered 11/7, 2009 at 18:19 Comment(2)
@Eusporangiate how can i set the screen off timeout to never?Eyecatching
I think this only works when lock screen is enabled. How to achieve it if lock screen is disabled?Ramp
D
27
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"
Dincolo answered 11/7, 2009 at 17:48 Comment(0)
L
14

Above is correct:

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

But also include permission in manifest:

android:name="android.permission.WRITE_SETTINGS"
Laskowski answered 29/5, 2012 at 7:31 Comment(0)
E
12

The Settings.System provider offers a SCREEN_OFF_TIMEOUT setting that might be what you are looking for.

Eusporangiate answered 11/7, 2009 at 18:19 Comment(2)
@Eusporangiate how can i set the screen off timeout to never?Eyecatching
I think this only works when lock screen is enabled. How to achieve it if lock screen is disabled?Ramp
Y
1

Here is a code-sheet, you can do more.

long stand = Settings.System.getLong(
                        mContext.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT,
                        -1);
                long sec = stand / 1000;
                String time = null;
                                    if(stand<0) {
                                         //close.
                                    }
                else if( sec >= 60) {//to minute
                    time = String.format(mContext.getString(R.string.minutes), (sec / 60) + "");
                } else {
                    time = String.format( mContext.getString(R.string.seconds),sec + "");
                }
Yoakum answered 24/11, 2013 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.