coder_For_Life22
coder_For_Life22

Reputation: 26991

How to cancel alarm after being set?

I am have a method that sets an alarm using information froma SQLite database i created.

public void scheduleItem(Long textId, Calendar when){
    Intent i = new Intent(mContext, onAlarmReceiver.class);
    i.putExtra(DbAdapter.KEY_ROWID, textId);
    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
    mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
    Log.e("SchedulerManager", "Task scheduled");

Now as you see this responds to a database. Now if the an item in the database is deleted by a user. I dont want the alarm to still be set for a item that has been deleted.

How can i pull back the alarm using the Long textId that is taken as a parameter in my scheduleItem() method?

EDIT: I just created this method to cancel a currently pending intent.

Maybe this will do it.

public void cancelAlarm(Long textId){
    Intent g = new Intent(mContext, onAlarmReceiver.class);
    g.putExtra(SmsDbAdapter.KEY_ROWID, textId);

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, g, 0);

    mAlarmManager.cancel(pi);

}

Upvotes: 0

Views: 7379

Answers (1)

Marco Grassi
Marco Grassi

Reputation: 1202

You have to invoke the cancel method: http://developer.android.com/reference/android/app/AlarmManager.html#cancel(android.app.PendingIntent)

with the same PendingIntent that you have setted before with alarm manager. (or better, an Intent that match what you have setted before)

Upvotes: 1

Related Questions