21

My requirement is: after a GCM message arrives, the device should wake up to display a high-priority notification. The device should turn the screen on.

Currently I'm using WakeLock to achieve this. The newWakeLock() method expects a lock level AND a flag to be passed (as the 1st param, bitwise or'd).

I'm using PowerManager.ACQUIRE_CAUSES_WAKEUP flag since it does exactly what I need. However, I'm a bit frustrated about the lock level. So according to the docs, I got the following options:

  • PARTIAL_WAKE_LOCK - not compatible with ACQUIRE_CAUSES_WAKEUP / doesn't turn the screen on
  • SCREEN_DIM_WAKE_LOCK - deprecated
  • SCREEN_BRIGHT_WAKE_LOCK - deprecated
  • FULL_WAKE_LOCK - deprecated

The suggested FLAG_KEEP_SCREEN_ON is completely useless in this scenario. I ended up just supressing the deprecation warning:

@SuppressWarnings("deprecation")
PowerManager.WakeLock screenOn = ((PowerManager) c.getSystemService(Context.POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
screenOn.acquire();
mNotifyMgr.notify(mNotificationId, mBuilder.build());
screenOn.release();

The question: is there a non-deprecated reliable way to wake up the device in the described case?

EDIT I'm not asking for workarounds to wake up the device. My question is whether this is possible to wake up the device from the background (without a running Activity) using no deprecated APIs

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • Related: http://stackoverflow.com/questions/30246425/turning-on-screen-from-reciever-service – Mr_and_Mrs_D May 21 '15 at 16:21
  • @Mr_and_Mrs_D thanks for the link, however the accepted answer is not suitable for my case since I have no `Window` object while the app is in the background and no `Activities` are running. So still looking for an answer – Droidman May 24 '15 at 22:30

1 Answers1

14

Use the code I got from my question, and then just finish the activity, should leave the screen on for the users normal amount of time. Trust me this is the only way, after spending a good week on this problem. You could always set the activity to transparent with notitlebar, the user will never know.

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().addFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    finish();
}
Oliv
  • 10,221
  • 3
  • 55
  • 76
RuAware
  • 979
  • 1
  • 9
  • 26
  • I'm not sure launching an `Activity` just to wake up the device is the correct and efficient way. And I'm definitely not not sure if Google considers this the correct way. I've updated the question to clarify. – Droidman May 25 '15 at 13:15
  • Ok, in that case the answer is no. I spent days trying it that way. I know open and close the activity within the oncreate. I have searched everywhere for another solution, it does not exist – RuAware May 25 '15 at 13:19
  • This is the only non-wakelock solution I have found too... I agree that this is not the best one, but as long as we don't have another (non-deprecated) solution this is the one we're bound to..... – techfly Nov 07 '16 at 11:50
  • This no longer seems to work on Android O? It only works if you get rid of finish(), but then the activity shows! – Flyview Nov 14 '17 at 00:57
  • I use the same piece of code, but without finishing the activity (I need to show a dialog), and it works fine for me too. However, 2 flags are deprecated now. I've described this with a partial solution as comment of the accepted answer here: https://stackoverflow.com/questions/30246425/turning-on-screen-from-receiver-service I'm aware it's not the solution asked (which required finishing the activity), but it might help with the 'deprecated' part of the question. – P Kuijpers Mar 06 '18 at 16:26
  • it's 2020 and still PowerManager.SCREEN_BRIGHT_WAKE_LOCK and WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON deprecated. – androidStud Jan 21 '20 at 02:30