45

Keeping the screen awake can be accomplished by using a wakelock by

mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
                getClass().getName());

Or by adding the FLAG_KEEP_SCREEN_ON to the window,

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

What is the technical difference between the two and in terms of performance and battery life which is recommended?

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155

3 Answers3

47

A wakelock gives you way more control (Like waking the phone to download something without turning the screen off) and requires your application to hold the wakelock permission.

Therefore FLAG_KEEP_SCREEN_ON is recommended if all you want is to keep the screen on while your window is visible.

mibollma
  • 14,959
  • 6
  • 52
  • 69
  • I just posted a question about this, as the FLAG_KEEP_SCREEN_ON only works when the "USB debugging" is checked... http://stackoverflow.com/questions/15189767/keep-screen-on-in-activity-does-not-work-with-flag-keep-screen-on – Ted Mar 03 '13 at 19:27
  • 10
    @Ted: What you've written here and in (at least) two other questions is just wrong. We should put it right for others: `FLAG_KEEP_SCREEN_ON` works perfectly for what it describes: It just makes the screen keep on. Neither is there any permission required for this to work nor does it only work if your device is in debug mode. It should work on every device, and if it doesn't, it's the device's fault ;) – caw Oct 30 '13 at 06:16
7

Wakelock is vague, since it has many different options. The flag FLAG_KEEP_SCREEN_ON only does that.

| Flag Value              | CPU | Screen | Keyboard |
-----------------------------------------------------
| PARTIAL_WAKE_LOCK       | On* | Off    | Off      |
| SCREEN_DIM_WAKE_LOCK    | On  | Dim    | Off      |
| SCREEN_BRIGHT_WAKE_LOCK | On  | Bright | Off      |
| FULL_WAKE_LOCK          | On  | Bright | Bright   |

Please see wakelock or PowerManager for Android specifics, and other answers for the exact implementation.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
5

Wake lock is used in background services to keep the CPU running to do work while the screen is off. You should never use wake lock in an activity. To use wake lock, WAKE_LOCK permission must be added in application's manifest file.


FLAG_KEEP_SCREEN_ON is used in activity to keep the screen turned on, which will also keep the CPU on without any special permission, unlike wake lock. You should never use FLAG_KEEP_SCREEN_ON in a service.

Cheers!

Mohan
  • 4,755
  • 2
  • 27
  • 20