본문 바로가기
Programming/Android Java

How to prevent screen timeout in Android

by 개Foot/Dog발?! 2014. 7. 18.

URL : http://thiranjith.com/2012/02/22/android-prevent-screen-timeout/


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).

1. Use of FLAG_KEEP_SCREEN_ON option for your Activity.
2. 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.

1-1. Using android:keepScreenOn XML attribute on the Activity’s layout


<!-- 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);
        // ...
    }


1-2. Using the FLAG_KEEP_SCREEN_ON flag within your Activity
You set up the flag in your Activity’s onCreate (or onStart depending on the desired effect) method to keep the screen on programatically



<!-- 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:

1) Grant the application permission to use WakeLocks by including the following in your manifest


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


2) Acquire a WakeLock.


    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    wl.acquire(); 


3) Release the WakeLock once no longer necessary. The screen will continue to stay on until it is released.


   wl.release();