I am using the Android AlarmManager to receive events at a specific time.
alarm_manager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC_WAKEUP, target_time, pending_intent);
The pending intent is created like this:
pending_intent = createPendingResult(PENDING_INTENT_ID, new Intent(), 0);
In order to make the alarm trigger while the phone is off, I acquire a partial wakelock:
power_manager = (PowerManager)getSystemService(POWER_SERVICE);
wake_lock = power_manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getString(R.string.app_name));
wake_lock.acquire();
Events are handled in the onActivityResult callback:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
My problem is that if I start the activity, then press the power button on my phone (which turns the screen off + locks it), the event will not be delivered until the user turns on the phone again, and unlocks the screen.
I assume it has something to do with the lock screen (maybe because the screen is locked, android figures the intent won't do anything anyway?) - but I find little to no information to my problem. With the partial wake lock, I would expect onActivityResult to be called, no matter if I turned off the screen, or whether it's locked - but obviously that's not the case.
Can anyone point me as to where I'm wrong?
Someone answered, but for some reason the answer disappeared, otherwise I would have accepted it.
The answer basically went that
pending_intent = createPendingResult(PENDING_INTENT_ID, new Intent(), 0);
is not reliable, and suggested using broadcast receivers instead. I still do not know why they aren't working (I commented on the answer asking if they knew anything more), but anyway. I wrote
broadcastReceiver = new AlertBroadcastReceiver(this);
registerReceiver(broadcastReceiver, new IntentFilter("SomeAction"));
and added a broadcast receiver to handle this, and this works.