Search code examples
javaandroidalarmmanagerandroid-pendingintentandroid-backup-service

Check if AlarmManager task is in progress


I am running periodic background task using AlarmManager:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent);

I put a flag PendingIntent.FLAG_UPDATE_CURRENT, but I afraid it re-writes my schedule if I run this code again. Questions:

  • Will it reset the time and I'll have to wait for 10 mins to receive an event?
  • Is there a way to check if my alarm task schedule is set?

Solution

  • Will it reset the time and I'll have to wait for 10 mins to receive an event?

    Yes it will. As per doc:

    If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

    And as per

    Is there a way to check if my alarm task schedule is set?

    Yes, there is several solutions, which you can check.

    And, btw, be aware that setRepeating is inexact.

    Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.