Android屏幕超时

时间:2009-07-11 17:47:52

标签: java android android-1.5-cupcake

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

4 个答案:

答案 0 :(得分: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"

答案 1 :(得分:14)

以上是正确的:

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

但也包括清单中的权限:

android:name="android.permission.WRITE_SETTINGS"

答案 2 :(得分:11)

Settings.System提供商提供的SCREEN_OFF_TIMEOUT设置可能正是您所寻找的。

答案 3 :(得分: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 + "");
                }