11

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

4

4 回答 4

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

并且不要忘记在清单中添加此权限:

android:name="android.permission.WRITE_SETTINGS"
于 2010-08-24T08:11:25.173 回答
14

以上是正确的:

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

但也包括清单中的权限:

android:name="android.permission.WRITE_SETTINGS"
于 2012-05-29T07:31:18.367 回答
12

Settings.System提供程序提供了一个SCREEN_OFF_TIMEOUT设置,这可能是您正在寻找的。

于 2009-07-11T18:19:03.310 回答
1

这是一个代码表,您可以做更多。

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 + "");
                }
于 2013-11-24T16:13:45.047 回答