So I've got a BroadcastReceiver and the AlarmManager.
Let's say I create the Pending Intents like so:
Intent i;
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_1);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent1 = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_2);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent2 = PendingIntent.getBroadcast(context, 2, i, PendingIntent.FLAG_UPDATE_CURRENT);
And schedule the alarms like so:
now = SystemClock.elapsedRealtime();
long time1 = now + 10 * 1000;
long time2 = time1 + 60 * 1000;
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time1, pendingIntent1);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time2, pendingIntent2);
I am now experiencing that my broadcast receiver gets the broadcast for ACTION_1
quite reliably, while ACTION_2
is often not being delivered. So onReceive
is rarely or never executed with an intent holding the action ACTION_2
. How comes that? I thought, *_WAKEUP
makes sure that broadcasts are being delivered anyway?
[Update 09/15/2015]
- For testing purposes I'm trying to print out a Log message in my onReceive
method. Still not working.
- I've tried using setExact on AlarmManager now. Still not working.
- I've even tried using WakefulBroadcastReceiver
. Still not working.
- I've found out, however, that the device reliably wakes up when in battery charged state.
What could cause this problem? I've read everywhere that broadcast receivers are guaranteed to be executed if triggered by the alarm manager via a pending intent (and not doing too much stuff in onReceive
). Do I maybe have some aggressive energy saving policy on my phone which I can't really work against (without acquiring a long wake lock, see comments)?
[Update 09/19/2015] I have just tested some alarm clock app (https://play.google.com/store/apps/details?id=com.alarmclock.xtreme.free) on Google Play and it does not reliably wake up the phone, too. I guess, it's really a bug and not my fault. I'll stick to the wake lock solution then, I guess.
showAt
? – Sam Dozortime1
. The app is basically about showing a dialog (in a transparent activity) after some time the screen went off (here 10 secs) and hiding it again, if the screen was off for too long (here 10+60=70 secs). – flxappswakeLock.acquire(time2 - now + 5000);
(keeping the phone awake for - in the upper example - 70 secs plus 5 secs as a buffer) - However, I feel like this is a rather hacky solution. What do you think? Is it legitimate or too dirty, probably causing problems? – flxapps