14

I am currently utilizing the below referenced code for a wake lock on an alarm notification activity. However, SCREEN_DIM_LOCK has been depreciated. So, what should I be replacing it with?

//Instance of wake lock for AlarmActivity
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyWakeLock");
user3391426
  • 433
  • 1
  • 5
  • 17

3 Answers3

14

Android Developer documentation specifies that SCREEN_DIM_WAKE_LOCK should be replaced with FLAG_KEEP_SCREEN_ON. After doing a bit of digging, I turned up this...

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

It should be placed in the onCreate() method.

user3391426
  • 433
  • 1
  • 5
  • 17
  • 11
    This is a replacement for `SCREEN_BRIGHT_WAKE_LOCK`, not for `SCREEN_DIM_WAKE_LOCK`. – Oliv Jun 30 '14 at 07:35
  • * @deprecated Most applications should use * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead * of this type of wake lock, as it will be correctly managed by the platform * as the user moves between applications and doesn't require a special permission. */ – Anthone Mar 18 '15 at 09:14
7

It can be replaced for FLAG_KEEP_SCREEN_ON, as the javadoc says, but this will prevent the screen from dimming - it will remain bright.

This API should not have been deprecated - it is still needed in some cases, such as the "dim" case.

See also this.

Community
  • 1
  • 1
Oliv
  • 10,221
  • 3
  • 55
  • 76
  • 1
    Nah, I prevents the screen from turning off in my experience, but does not prevent dimming. I have a device beside me right now; screen dimmed, but did not turn off. – Ted Oct 23 '22 at 07:35
-1

Just use

WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

In place of

PowerManager.SCREEN_DIM_WAKE_LOCK
Kailash Uniyal
  • 878
  • 1
  • 10
  • 14