[TUT] Keep Screen on/awake – 3 possible ways

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:

  1. Declare the screen stays on in your XML layout
  2. Inform the window manager in onCreate you want the screen to stay on
  3. 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).

package com.blundell.tut.ui.phone;

import com.blundell.tut.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

/**
 * 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
 * @author paul.blundell
 *
 */
public class ScreenOnWakeLockActivity extends Activity {
    private static final String TAG = "com.blundell.tut.ui.phone.ScreenOnWakeLockActivity.WAKE_LOCK_TAG";
	private WakeLock wakeLock;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wake_lock);

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	wakeLock.acquire();
    }

    @Override
    protected void onPause() {
    	super.onPause();
    	wakeLock.release();
    }
}

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.

MainActivity.java

package com.blundell.tut.ui.phone;

import com.blundell.tut.R;
import com.blundell.tut.annotations.FromXML;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @FromXML
    public void onFlagActivityClick(View v){
    	Intent intent = new Intent(this, ScreenOnFlagActivity.class);
    	startActivity(intent);
    }

    @FromXML
    public void onXmlActivityClick(View v){
    	Intent intent = new Intent(this, ScreenOnXmlActivity.class);
    	startActivity(intent);
    }

    @FromXML
    public void onWakeLockActivityClick(View v){
    	Intent intent = new Intent(this, ScreenOnWakeLockActivity.class);
    	startActivity(intent);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onFlagActivityClick"
        android:text="Activity - Flag" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onXmlActivityClick"
        android:text="Activity - Xml" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onWakeLockActivityClick"
        android:text="Activity - WakeLock" />

</LinearLayout>

Finally the AndroidManifest, the wakelock permission is needed, for the WakeLockActivity only, other than that this is just standard.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.blundell.tut"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ui.phone.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ui.phone.ScreenOnFlagActivity" />
        <activity android:name=".ui.phone.ScreenOnXmlActivity" />
        <activity android:name=".ui.phone.ScreenOnWakeLockActivity" />
    </application>

</manifest>

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.

Source Downloads:

Eclipse source project: here

GitHub Repo: here

Hope you are enlightened, any questions just ask.

2 thoughts on “[TUT] Keep Screen on/awake – 3 possible ways

  1. Thank you SO much for this Blundell … silly me just spent the last 3 hours making sure I had my timers/wakelocks right under every possible scenario! I just needed to find out how to stop the screen DIMMING while the user is watching a slideshow and I came across YOUR post. Back to the drawing board for me … I can do all that with ONE line of code … gotta love WindowManager =)

  2. I like the PowerManager.SCREEN_DIM_WAKE_LOCK example, but this method is deprecated from API 17. How do you set the screen on globally from API 17 onwards?

Comments are closed.