0

I'm building an alarm app. I have it so a broadcastreceiver acquires a wakelock and starts the alarm activity. These are the flags I'm using:

Wakelock:

PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                                   PowerManager.ACQUIRE_CAUSES_WAKEUP |
                                   PowerManager.ON_AFTER_RELEASE, ctx.getPackageName());
wakeLock.acquire();

Activity:

getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON + 
                     LayoutParams.FLAG_DISMISS_KEYGUARD +
                     LayoutParams.FLAG_SHOW_WHEN_LOCKED + 
                     LayoutParams.FLAG_TURN_SCREEN_ON);

What happens is that the activity gets called correctly, but then the lock screen gets called which fires the onPause on my activity. My activity then shows in front of the lock screen. So the overall behavior is the screen flickers on, then off, then back on. This causes my activity to go onCreate -> onResume -> onPause -> onResume. I would like to avoid the onPause if possible. Any suggestions?

Huy T
  • 1,273
  • 1
  • 11
  • 21
  • Alarm manager -> BroadcastReceiver -> `onReceive(){/* your wakelock code*/}`? – Mr_and_Mrs_D Nov 22 '13 at 15:38
  • That is correct. After the wakelock code, the onReceive calls the Activity. – Huy T Nov 23 '13 at 02:15
  • Is the wakelock a field of the receiver ? or a local variable ? – Mr_and_Mrs_D Nov 23 '13 at 05:14
  • Yes, the wakelock is a field of the receiver. – Huy T Nov 25 '13 at 23:38
  • Then probably the phone is falling asleep again once onReceive returns - can it be the case ? – Mr_and_Mrs_D Nov 26 '13 at 02:05
  • Yes, that is the case, but only when the lock screen is present. Without a lock screen, it doesn't do that. – Huy T Nov 28 '13 at 00:47
  • So you need to keep the phone awake till your activity starts ? – Mr_and_Mrs_D Nov 28 '13 at 00:50
  • That's not the issue. The issue is that the lock screen is stealing away the foreground focus, thus making my activity do onCreate -> onResume -> (lock screen steals focus) -> onPause -> (activity dismisses lock screen) -> onResume. – Huy T Dec 03 '13 at 02:12
  • I suspect the behavior is because the phone falls asleep again and the lock screen pops up - to avoid this you need to start your activity with a static method from the receiver to acquire a static wake lock belonging to your activity. On the other hand maybe a simple `lock.disableKeyguard();` in your onResume() would suffice ? `KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); lock.disableKeyguard();` – Mr_and_Mrs_D Dec 03 '13 at 03:08

0 Answers0