0

I want to do following:

1) cancel an alarm

2) check if an alarm exists...

To test this, I do following:

 public static void startUpdater(Context context)
{
    // START TEST CODE
    cancelAllAlarms(context);
    alarmExists(context);
    // END TEST CODE

    if (true)// !alarmExists(context))
    {
        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = getAlarmPendingIntent(context);

        Calendar calNextAlarm = Calendar.getInstance();
        calNextAlarm.set(Calendar.HOUR, 12);
        calNextAlarm.set(Calendar.MINUTE, 0);
        calNextAlarm.set(Calendar.SECOND, 0);
        calNextAlarm.set(Calendar.MILLISECOND, 0);

        L.d(Updater.class, "Alarm wird registriert..." + calNextAlarm.getTime().toLocaleString());

        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                calNextAlarm.getTimeInMillis(),
                86400000, // repeat once a day
                pi);
    }
}

private static void cancelAllAlarms(Context context)
{
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingUpdateIntent = getAlarmPendingIntent(context);
    try
    {
        alarmManager.cancel(pendingUpdateIntent);
        L.d(Updater.class, "AlarmManager canceled!");
    }
    catch (Exception e)
    {
        L.d(Updater.class, "AlarmManager update was not canceled. " + e.toString());
    }
}

private static Intent getAlarmIntent(Context context)
{
    return new Intent(context, AlarmReceiver.class);
}

private static PendingIntent getAlarmPendingIntent(Context context)
{
    return getAlarmPendingIntent(context, false);
}

private static PendingIntent getAlarmPendingIntent(Context context, boolean flagNoCreate)
{
    Intent i = getAlarmIntent(context);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, flagNoCreate ? PendingIntent.FLAG_NO_CREATE : 0);
    return pi;
}

private static boolean alarmExists(Context context)
{
    boolean exists = getAlarmPendingIntent(context, true) != null;
    L.d(Updater.class, "AlarmManager exists: " + exists);
    return exists;
}

But after cancelling the alarm, my check functions still returns true... Can someone point out my error?

prom85
  • 16,896
  • 17
  • 122
  • 242

2 Answers2

0

Try this

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);         

Instead of this

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i,
            flagNoCreate ? PendingIntent.FLAG_NO_CREATE : 0);
Shini
  • 573
  • 4
  • 11
  • I tried setting this flag for the `PendingIntent` in my cancel function but still afterwards my check functions returns true... – prom85 May 12 '14 at 09:43
  • 1
    http://stackoverflow.com/questions/21508611/alarm-cancel-button-not-working-correctlysolved – Shini May 12 '14 at 12:19
  • that helped... For checking and canceling, I need to create the `PendingIntent` with the flag `FLAG_NO_CREATE`. In my case, I'm always creating an `PendingIntent` in my cancel function and therefore, afterwards my check function returns true – prom85 May 12 '14 at 12:30
0

I'd just like to say I was struggling with this problem recently and discovered that I needed to cancel the PendingIntent as well as the Alarm:

pi.cancel();
alarmManager.cancel(pi);

I'm not sure if that's negated by the advice felix gave but whenever I was creating a PendingIntent (with the aim of cancelling an alarm) I always did it the exact same way I did when creating the alarm with FLAG_UPDATE_CURRENT. For example:

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

Hope that helps.

dan0xii
  • 11