Search code examples
androidalarmmanager

setRepeating won't cancel from other activity


I see there is a lot of similar questions but I have my alarm info stored in a database. The reminder is cancelled inside the database by a call to deleteReminder and it looks like this

 public boolean deleteReminder(long rowId) {

    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;


} 

This deletes the reminder but not the alarm. I tried changing it to

public boolean deleteReminder(long rowId) {

    new ReminderManager(this).cancelReminder();


    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

}

and added a cancelReminder method in my ReminderManager activity like this

public void setReminder(Long taskId, Calendar when, String spinInterval) { Log.e(TAG, spinInterval);

    long l = Long.parseLong(spinInterval);



    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

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

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

public void cancelReminder(){

      Intent i = new Intent(mContext, OnAlarmReceiver.class);
      PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);

      mAlarmManager.cancel(pi);

     }

but it didn't work.... Can you show me the correct code to make the alarm stop repeating?


Solution

  • Use PendingIntent.FLAG_UPDATE_CURRENT for both calls to PendingIntent.getBroadcast(). The code you posted uses PendingIntent.FLAG_ONE_SHOT the second time you call it.