Sunday, July 3, 2011

Forcing screen to stay on

Here's a nice way to make the screen stay on for a particular activity. Just put this in the onCreate method:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


It's nicer than the POWERLOCK method, which requires an additional permission in the manifest:

    <uses-permission android:name="android.permission.WAKE_LOCK">

And in your Activity:

    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    this.mWakeLock.acquire();

    //and when you're done with the WakeLock:
    this.mWakeLock.release();

No comments:

Post a Comment