0

I am building a task reminder in Java (using Eclipse). The task reminder needs to prompt a reminder at a specific time before the task. Additionally, it needs to repeat every X minutes before the task, and finally at the specific time that the task reminder was set it should then send one last reminder.

For example:

I set a task with title "Deadline JAVA" at "17:00 27-3-2015". I need to set an alarm at "16:00 27-3-2015", and every 5 minutes until "17:00 27-3-2015".

I have this setReminder method:

new ReminderManager(this).setReminder(mRowId, rCalendar, mCalendar);

mRowId is the ID from the task, rCalendar is the time BEFORE the task, so 16:00, and mCalendar is the final reminder time.

I'm using this code for setReminder:

public void setReminder(Long taskId, Calendar when, Calendar when2) {
    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long) taskId);

    int ID = taskId.intValue();

    PendingIntent pi = PendingIntent.getBroadcast(mContext, ID, i,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            when.getTimeInMillis(), 300000 , pi);
}

This works. The alarm is starting, and repeating every 5 minutes. Now the hardest part, and where I need some help is to send one final alarm and have the alarm stop repeating. How do I do this?

Edit: Code for mAlarmManager

private AlarmManager mAlarmManager;
public ReminderManager(Context context) {
    mContext = context;
    mAlarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
}
Joeeeeh
  • 1
  • 3

1 Answers1

0

Since you're using the AlarmManager class from the Android library this isn't too difficult.

In order to do this you'll need to track when the repeating alarm goes off in order to capture if that should have been the 'last one.' Once you determine that the last one as be activated, you can call this:

mAlarmManager.cancel(pi);

The cancel operation takes a PendingIntent and cancels any alarm that has that same PendingIntent instance set. This will stop the alarm from continuing after that last one goes off.

Here's a related post regarding alarm cancellation: Delete alarm from AlarmManager using cancel() - Android

Community
  • 1
  • 1
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • And that is my big question. How do i track when the repeating alarm goes off to capture wich one is 'the last one'? – Joeeeeh Mar 27 '15 at 17:50
  • There are few ways to do it. One possibility is to write a service and intercept the alarm going off and increment a counter. Another way would be to use a database like SQLite and update a record whenever the alarm goes off by writing that into the activity or fragment that gets activated in the PendingIntent – JNYRanger Mar 27 '15 at 17:55