Android Developer Tutorials and Blog

This tutorial will show you how to stop the screen timing out for an android activity so you can continue to show some information without user interaction and without the screen going off.

Dont use a WakeLock when you don’t have to, unless you specifically need to force the device away (processor and all).

A scenario for keeping the screen on could be something like a cooking recipe application, wanting to leave the recipe on the screen without the screen turning off and without the user having to press the screen every few minutes to keep it active.

There are 3 possible ways of doing this, two are preferable and similar in function it’s just personal preference which you use. The other is a bit more aggressive and can be used in other scenarios where you want other parts of the system to stay awake as well (like the processor).

Here’s the ways:

Declare the screen stays on in your XML layout

Inform the window manager in onCreate you want the screen to stay on

WakeLock – used for critical downloads or things that you definitely don’t want the Android system shutting down for

Watch out for those pesky wakelocks only use when necessary. Ref: Battery Killers

ScreenOnFlagActivity.java
This Activity below keeps the screen on using the window manager, you don’t have to worry about managing this it will be kept on for the duration of the Activity lifecycle. The screen may dim but it won’t turn off. No permissions are needed in your manifest.

package com.blundell.tut.ui.phone;
import com.blundell.tut.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
/**
* This Activity keeps the screen on using the window manager, you don't have to worry about managing this
* it will be kept on for the duration of the Activity life.
* No permissions are needed in your manifest.
* @author paul.blundell
*/
public class ScreenOnFlagActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}

ScreenOnWakeLockActivity.java
This Activity aquires a wakelock to keep the screen on whilst in this activity. This requires a permission in your manifest. It is important to manage your wakelocks and always release them when finished (in onPause).

ScreenOnXmlActivity.java
This activity keeps the screen on using a flag in the XML layout file we are using ‘android:keepScreenOn=”true”‘ no manifest permissions are needed.

package com.blundell.tut.ui.phone;
import com.blundell.tut.R;
import android.app.Activity;
import android.os.Bundle;
/**
* This activity keeps the screen on using a flag in the XML layout file we are using 'android:keepScreenOn="true"'
* We don't use a wakelock so no manifest permissions are needed
*
* @author paul.blundell
*
*/
public class ScreenOnXmlActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
}
}

The main activity is the navigation to get to each, note it uses the Annotation discussed in this other blog post, this highlights the use of xml onclicks and gives visibility of where the methods are called.

Thats it! Make sure you use one of these methods that is relative to what you want to achieve, don’t over use wakelocks (and avoid when possible). Not having the permission in your manifest is more likely a user will continue with the install once they get to this screen.