Java Android 屏幕超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1114270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:55:27  来源:igfitidea点击:

Android Screen Timeout

javaandroidandroid-1.5-cupcake

提问by Tom

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.

我知道可以使用唤醒锁来保持屏幕、CPU 等,但是我如何以编程方式更改Android 手机上的“屏幕超时”设置。

采纳答案by CommonsWare

The Settings.Systemprovider offers a SCREEN_OFF_TIMEOUTsetting that might be what you are looking for.

Settings.System提供商提供了一个SCREEN_OFF_TIMEOUT设置,可能是你在找什么。

回答by 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"

回答by 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"

回答by TeeTracker

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 + "");
                }