0

I have a service that listens to push notifications and creates an activity. Everything works well except when the device's screen is off. The desired behavior is that a notification would wake the screen up to the locked state homescreen and display the notification.

I am only able to create this behavior with:

mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                           PowerManager.ACQUIRE_CAUSES_WAKEUP |
                           PowerManager.ON_AFTER_RELEASE,
                           WAKELOCK_KEY);

However, I see that Android has deprecated FULL_WAKE_LOCK in favor of FLAG_KEEP_SCREEN_ON. However, I have tried this in the activity that is called from the background service:

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

And my logs show that this activity is being created but the screen still does not display this activity. If I press the power button, the display shows the activity immediately proving that it was created. Here is a summary of some of the things I have tried:

  1. FULL_WAKE_LOCK (works but is deprecated)
  2. WindowManager - FLAG_TURN_SCREEN_ON, FLAG_KEEP_SCREEN_ON (doesn't work)
  3. Google WakefulBroadCastReceiver (doesn't work)

Are there any alternatives to FULL_WAKE_LOCK?

Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49

2 Answers2

0

Waking up (SCREEN_ON) device from service works with

newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, "MyTag").

Tested on Android 4.1, 4.4, 5.0

In my app i am waking up the device via the AlarmManager and registering a broadcast event. Be sure to acquire wakelock in the WakefulBroadCastReceiver before starting service and release the lock not in the same callstack in the service as doing this will reside the device in SCREEN_OFF.

Dokumans
  • 359
  • 1
  • 3
  • 14
-1

This is what Android Clock do in the onCreate method:

    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    // Turn on the screen unless we are being launched from the AlarmAlert
    // subclass as a result of the screen turning off.
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

Are you using the first set of flags?

Edit: I've just tested it and it works.

user2715109
  • 351
  • 6
  • 16