0

My alarm clock app uses AlarmManager to set alarms and I have a problem with PendingIntents being cancelled when I close the app from recent app list (it happens occasionally on a KitKat device and almost constantly on Marshmallow), even if the alarm was set a couple minutes ago (so device is not asleep).

Here's how I set an alarm:

Calendar alarmCalendar = getCalendarForDay(day);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            (id * 10000 + day.position), getIntent(context, day.position), FLAG_UPDATE_CURRENT);

long intervalMsec = alarmCalendar.getTimeInMillis();
if (android.os.Build.VERSION.SDK_INT >= 19) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, intervalMsec, pendingIntent);
} else alarmManager.set(AlarmManager.RTC_WAKEUP, intervalMsec, pendingIntent);

getIntent():

private Intent getIntent(Context context, int dayPosition) {
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(MainActivity.PACKAGE_NAME + ".alarmId", id);
    intent.putExtra(MainActivity.PACKAGE_NAME + ".dayPosition", dayPosition);
    return intent;
}

I've spent quite a while browsing this site and found some explanations here and here (basically says that it's normal and some manufacturers just do that), but none of them have actually helped me to deal with the problem. Still, I'm pretty sure that canbe solved, since there are some apps that work on all devices (such as Timely and other popular alarm clocks).

So does anyone have any thoughts on how those alarms actually deal with AlarmManager? Why their intents are never get cancelled?

Community
  • 1
  • 1
ulmaxy
  • 820
  • 2
  • 14
  • 22

0 Answers0