2009-07-11 147 views
9

我知道可以使用喚醒鎖來保持屏幕,CPU等,但我怎樣才能以編程方式更改Android手機上的「屏幕超時時間」設置。Android屏幕超時

回答

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" 
13

以上是正確的:

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

而且還包括在清單權限:

android:name="android.permission.WRITE_SETTINGS" 
1

這是一個代碼表,你可以做mo回覆。

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