Delete Alarm from Alarmmanager Using Cancel() - Android

How to cancel alarm from AlarmManager

The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).

For a quick fix, this should work:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);

alarmManager.cancel(pendingIntent);

In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:

Setting:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

Cancelling:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

How to cancel alarm from AlarmManager

The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).

For a quick fix, this should work:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);

alarmManager.cancel(pendingIntent);

In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:

Setting:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

Cancelling:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

Delete alarm from AlarmManager using cancel() - Android

Try this flag:

PendingIntent.FLAG_UPDATE_CURRENT

Instead of:

PendingIntent.FLAG_CANCEL_CURRENT 

So the PendingIntent will look like this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 
alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT)

(Make sure that you use same alert object and mContext!)

A side note: If you want one global AlarmManager, put the AlarmManager in a static variable (and initialize it only if it's null).

Deleting an alarm with alarmManager.cancel after alertdialog

In your AddListActivity, declare a private static member of type Context:

private static Context mContext;

In the onCreate() method of AddListActivity, initialize it:

mContext = this;

In the code block to build the PendingIntent and cancel the alarm, replace AddListActivity.this with mContext.

Delete specific Alarm from AlarmManager

RequestCode parameter of getBroadcast method uniquely identify each PendingIntent.

You could assign an Unique integer code for each PendingIntent.

Alarm1:

      PendingIntent pi = PendingIntent.getBroadcast(this.context, 1, ringintent, 0); 

Alarm2:

      PendingIntent pi = PendingIntent.getBroadcast(this.context, 2, ringintent, 0); 

Now if you want to kill Alarm2 alone, recreate the PendingIntent with same identifier and then call cancel method of AlarmManager

      PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);

How to correctly cancel an AlarmClock alarm set by AlarmManager in the Clock app?

So, I found that I needed to use ACTION_DISMISS_ALARM instead of ACTION_SET_ALARM. It lets you cancel already set alarms in the alarm clock.

The answers here, here, and here suggested cancelling the PendingIntent which didn't work for me and was not confirmed to be working for the question authors either.

The explanation for why cancelling the PendingIntent from AlarmManager doesn't work, I think, could be that a PendingIntent is there to let you do something or send the intent later on. Once you've already done it (already sent the intent) you can't undo it. For example, you can cancel a notification, but can't undo opening the app from the notification (the intent or action performed) as it's already done. Moreover, in my case, it was another app that I needed to unset the alarm for so maybe I shouldn't have expected to be able to undo that action.
So in order to dismiss or cancel the alarm, you need to send a new intent with the action ACTION_DISMISS_ALARM.

Replacing the try/catch block as follows sets and cancels the alarm correctly for me:

try {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return;
}
Thread.sleep(15000);
Intent ci = new Intent(AlarmClock.ACTION_DISMISS_ALARM);
ci.setData(i.getData());
alarmIntent = PendingIntent.getActivity(this, 0, ci, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
alarmIntent);
Log.i(TAG, "Alarm cancelled");
} catch (InterruptedException e) {
e.printStackTrace();
}

However, this only works for API level 23+ and doesn't let you SKIP_UI like in ACTION_SET_ALARM.

Delete specific ALARM from AlarmManger in Android

by Id that you set in alarm manager you can access specific alarm, in your code:

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);

Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);

at this line choose ID separate from each other , then you can get specific alarm by it's id

PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), ID, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);

Delete Alarm with a unique ID

AlarmManager alarmManager = (AlarmManager) 
getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(),
SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);

alarmManager.cancel(pendingIntent);

See How to cancel alarm from AlarmManager link for more info.

When deleting alarm from AlarmManager, should I also cancel PendingIntent?

It is not necessary to do this. But it matters upon your usage.

A PendingIntent itself is simply a reference to a token maintained by
the system describing the original data used to retrieve it. This
means that, even if its owning application's process is killed, the
PendingIntent itself will remain usable from other processes that have
been given it
. If the creating application later re-retrieves the same
kind of PendingIntent (same operation, same Intent action, data,
categories, and components, and same flags), it will receive a
PendingIntent representing the same token if that is still valid, and
can thus call cancel() to remove it.

If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.



Related Topics



Leave a reply



Submit