Search code examples
androidalarmmanagerandroid-alarms

Getting false positive while checking whether alarm manager is set


I have two alarms 1. walk reminder 2. Tips Notification The user can cancel, the walk reminder. The canceling process is working fine.

private void setWalkReminder(){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 18);
    calendar.set(Calendar.MINUTE, 50);
    calendar.set(Calendar.SECOND, 0);
    Intent walkIntent = new Intent(this, AlarmReceiver.class);
    Log.d(TAG, "Started WALK SET");
    walkIntent.putExtra("alarmType", "walkReminder");
    PendingIntent walkPendingIntent = PendingIntent.getBroadcast(this, WALK_REMINDER, walkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager walkAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    walkAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 180000 /*AlarmManager.INTERVAL_DAY*/, walkPendingIntent);

    workoutEditor = workoutPrefs.edit();
    workoutEditor.putBoolean("walkReminderIsSet", true);
    workoutEditor.commit();
}

private void cancelWalkReminder(){
    Intent walkIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent walkPendingIntent = PendingIntent.getBroadcast(this, WALK_REMINDER, walkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.d(TAG, "Walk Canceled");
    AlarmManager walkAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    walkAlarmManager.cancel(walkPendingIntent);
}

I am checking whether the alarm is set by the following method:

boolean walkAlarmDown = (PendingIntent.getBroadcast(this, WALK_REMINDER,
            new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) == null);

    if (walkAlarmDown && workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
        Log.d(TAG, "WALK REMINDER SET");
        setWalkReminder();
    } else if (!walkAlarmDown && !workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
        Log.d(TAG, "WALK REMINDER CANCELED");
        cancelWalkReminder();
    } else {
        if(!workoutPrefs.getBoolean("walkReminderIsNeeded", true) && walkAlarmDown){
            Log.d(TAG, "Why is this false?");
        } else if (!walkAlarmDown && workoutPrefs.getBoolean("walkReminderIsNeeded", true)){
            Log.d(TAG, "proper functioning");
        }
}

if the user re-enables the reminder after it was canceled, then the reminder does not get set again . Log.d output is proper functioning. This is because walkAlarmDown still gives false. I believe this is because walkAlarmDown did not become true when the alarm was canceled. How to ensure walkAlarmDown should become true when alarm is canceled?

Alernatively, is it remaining true because my other alarm is enabled? Hence, only one alarm is canceled and another alarm remains active. It has a different Broadcast request code. So I presume it should not be an issue.


Solution

  • walkPendingIntent is still active even if the alarmmanager is canceled. The code, walkAlarmManager.cancel(walkPendingIntent);, should be followed by walkPendingIntent.cancel(). Now walkAlarmDown will give a true after canceling the alarm.