I have created a pending intent that fires a repeating alarm every minute. The alarm works fine but when I cancel the alarm, it still fires. I have read other posts about this and I made sure that everything is as suggested, mainly to use FLAG_UPDATE_CURRENT
and to use the same code for the intent, when creating and when cancelling. Here is the code I use:
Create the alarm:
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.add(Calendar.MINUTE, 1);
alarmManager.setRepeating(AlarmManager.RTC, c.getTimeInMillis(), 1000 * 60, getIntent(context));
Cancel the alarm:
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
getIntent(context).cancel();
alarmManager.cancel(getIntent(context));
The getIntent function:
public static PendingIntent getIntent(Context context) {
return PendingIntent.getBroadcast(context, 20, new Intent(ACTION_MINUTE_ALARM), PendingIntent.FLAG_UPDATE_CURRENT);
}
Does anyone know what the problem is?
After a lot of struggle I discovered that there was another service running (different apk) that was also creating exactly the same pendingIntent using a repeating alarm. Removing that service fixed the problem. Therefore, the method described in the original question works. Hope this helps someone.