There are several mechanisms available for Android applications to stop the phone’s screen from being turned off. These methods override the default ‘screen timeout’ behaviour of your phone (typically found under Settings -> Display
), and will keep the screen on while your application is running.
I will be introducing two methods to stop the screen from being dimmed. The two methods provide means of accomplishing this depending on the current state of your application; i.e. whether it has an Activity on the foreground (visible on screen) or running on the background (as a Service).
- Use of
FLAG_KEEP_SCREEN_ON
option for your Activity. - Use of a WakeLock.
1. Use of FLAG_KEEP_SCREEN_ON option
This is the recommended way to prevent screen timeout for an Android Activity. Doing so would keep your screen turned on as long as your Activity is visible (i.e. on the foreground).
The advantages of this method include:
- Do not need any special permissions to implement the solution
- Gives you greater control by having the screen on only for selected Activities, rather than for the whole application.
- A more power-efficient solution compared to alternate methods.
- Developer does not have to explicitly manage screen visibility, as the behaviour will automatically be reverted back to default once the Activity is no longer visible on screen (e.g. user pressed the back button, or another application was opened on top).
There are two ways that you can achieve this.
- Using
android:keepScreenOn
XML attribute on the Activity’s layout (see line #5).You don’t have to do anything special in the Activity class. Another benefit of this particular method is that you can control screen timeout on a per View basis as opposed to per-Activity basis.
<!-- main_layout.xml file --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:keepScreenOn = "true" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <!-- other views ommitted for brievity --> </LinearLayout>
public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); // ... } }
- Using the
FLAG_KEEP_SCREEN_ON
flag within your ActivityYou set up the flag in your Activity’s onCreate (or onStart depending on the desired effect) method to keep the screen on programatically. This allows you the option to easily switch back to the default screen timeout behaviour by invoking the stopForceScreenOn() method in the example below.
<!-- main_layout.xml file --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <!-- other views ommitted for brievity --> </LinearLayout>
public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } private void stopForceScreenOn() { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }
2. Use of a WakeLock.
A WakeLock can be used by your application to turn the screen on (amongst other things), and keep it on until the WakeLock is released. Use of WakeLocks drain your battery life significantly. Therefore WakeLocks are only recommended to be held for short periods of time and must be released as soon as possible.
Moreover, use of a WakeLock is the only way to programatically turn the screen on when the phone is already sleeping (e.g. when responding to a message received over the web etc.).
Using a WakeLock is a 3-step process consisting of the following:
- Grant the application permission to use WakeLocks by including the following in your manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
- Acquire a WakeLock.
The code snippet below demonstrate how to obtain a WakeLock that will cause the screen to be lit (but dimmed) until the lock is released.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire();
- Release the WakeLock once no longer necessary. The screen will continue to stay on until it is released.
wl.release();
Please refer to the Android SDK documentation if you want to know more about WakeLocks.
Conclusions:
As a rule of thumb, don’t forcefully keep the screen on for long periods unless you have a visible Activity. Having the screen on will use more power regardless of the method you use to accomplish it!
Due to the high power consumption by WakeLocks, I would recommend using them only via background Services to give a cue to the user. You must ensure that the WakeLock is released soon to prevent battery drain.
If you want to keep the screen on when running an Activity, always use the FLAG_KEEP_SCREEN_ON
flag (either in your Activity or in XML layout).
As always, please let me know if you have any questions and your feedback is always appreciated!
Thanks!