Reputation: 5850
The PendingIntent
cancel()
API doc says:
Cancel a currently active PendingIntent. Only the original application owning a PendingIntent can cancel it.
I not sure about the meaning of this. If I set the AlarmManager event from activity x
like this:
PendingIntent pendingIntent;
Intent myIntent = new Intent(x.this, AlarmReciever.class);
myIntent.putExtra("task_uuid", task_uuid);
pendingIntent = PendingIntent.getBroadcast(x.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, dateTime.getTimeInMillis(), pendingIntent);
My question is: can I cancel the pending intent from activity y
using:
PendingIntent pendingIntent;
Intent myIntent = new Intent(y.this, AlarmReciever.class);
myIntent.putExtra("task_uuid", task_uuid);
pendingIntent = PendingIntent.getBroadcast(y.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Upvotes: 2
Views: 474
Reputation: 1273
Brief Overview
AlarmManager basically schedules a time based operation by invoking the PendingIntent at the scheduled time. So inorder to cancel the Scheduled Alarm you need access to that PendingIntent.
Always note the two parameters while creating a Pending Intent- Request Code and Flag
PendingIntent.getBroadcast(context,REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);
This brief overview is not enough to understand AlarmManager. Read more about working of AlarmManager and PendingIntent here
Solution
You can cancel the scheduled Alarm from anywhere inside your Application even if dont have access to the same PendingIntent object used while scheduling the Alarm. You can create a new pending intent with the same request code and FLAG_NO_CREATE and it will return the same PendingIntent object.
/*
With FLAG_NO_CREATE it will return null if the PendingIntent doesnt already
exist. If it already exists it returns
reference to the existing PendingIntent
*/
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,REQUEST_CODE,intent,PendingIntent.FLAG_NO_CREATE);
if (pendingIntent!=null)
alarmManager.cancel(pendingIntent);
Upvotes: 0
Reputation: 22493
When you set alarm you need to pass one key value in PendingIntent it will differentiate no of alarms, should be like this
pendingIntent = PendingIntent.getBroadcast(x.this, key_value, myIntent,0);
SharedPreferences settings = context.getSharedPreferences("alarm", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, false);
editor.commit();
to cancel same alarm you need to save that key_values
some where, you can use Shared preferences. get the same key and then cancel alarm like this
SharedPreferences settings = context.getSharedPreferences("alarm", 0);
Map<String,?> allNotifyIdsMap = settings.getAll();
if(allNotifyIdsMap!=null&&allNotifyIdsMap.isEmpty()==false)
{
for(String notifyId: allNotifyIdsMap.keySet())
{
boolean isCleared = settings.getBoolean(notifyId, false);
if(isCleared==false)
{
pendingIntent = PendingIntent.getBroadcast(y.this, key_value, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
}
}
Upvotes: 1