Android屏幕超时

我知道它可能使用唤醒锁屏幕,CPU等,但我怎样才能以编程方式更改Android 屏幕上的“ 屏幕超时 ”设置。

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

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" 

以上是正确的:

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

但也包括清单许可:

 android:name="android.permission.WRITE_SETTINGS" 

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

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