Android Screen Timeout

↘锁芯ラ 提交于 2019-11-27 13:19:46

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

user788435
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"
Shadab

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"

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 + "");
                }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!