2009-07-11 11 views
9

Ich weiß, dass es möglich ist, ein Wakelock zu verwenden, um den Bildschirm, CPU, ect auf, aber wie kann ich die Einstellung "Screen Timeout" auf einem Android-Handy programmgesteuert ändern.Android Bildschirm Timeout

Antwort

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

Und auch vergessen Sie nicht, diese Erlaubnis in Manifest hinzuzufügen:

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

Above korrekt ist:

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

Aber auch enthalten Erlaubnis in manifest:

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

Hier ist ein Code-Sheet, Sie können mo Re.

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