Search code examples
javaandroidalarmmanagerandroid-pendingintent

Cannot cancel alarmmanager


I'm trying to create 3 notifications after the desired date and time is selected from a datepicker and a timepicker. I want to send notifications for the last 3 days for the selected value. But after the 3 notifications I want to cancel the notification process. I set up my pending notification and an alarm manager from my activity like

TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        //last 3 days add notification
        Calendar calendar =  Calendar.getInstance();
        calendar.set(year, month, day, hourOfDay, minute, 0);
        long when = calendar.getTimeInMillis();
        when = when -3*(24*60*60*1000)+3000; //set startup time 3 days before
        GetTime(when);
        long diff = when - Calendar.getInstance().getTimeInMillis();
        while (diff < 0){
            when+= (24*60*60*1000); //increment 24h if selected time is closer then 3 days
            diff = when - Calendar.getInstance().getTimeInMillis();
            GetTime(when);
        }

        if (diff >0) {
            Intent notifyIntent = new Intent(getApplicationContext() ,NotificationReceiver.class);
            notifyIntent.putExtra("ctg", categorySelected);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), new Utils().getIdForCategory(categorySelected), notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, 60*1000, pendingIntent); //setting for testing purposes for 60 seconds
        }
    }
};

My receiver class is :

protected void onHandleIntent(Intent intent) {
    String categorySelected = intent.getStringExtra("ctg");
    SharedPreference pref = new SharedPreference(getApplicationContext());
    int alarmCount = pref.getAlarmCount(categorySelected);
    if (alarmCount<=3){
        Log.d("alarmCount", String.valueOf(alarmCount));
        pref.saveAlarmCount(categorySelected,alarmCount+1);
        showNotification(intent);
    }
    else //we reached the 3 times
    {
        new Utils().clearAllPendingIntent(getApplicationContext(),categorySelected);
    }
}

and my clear method

public void clearAllPendingIntent(Context context, String categorySelected){
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent notifyIntent = new Intent(context ,NotificationReceiver.class);
    notifyIntent.putExtra("ctg",categorySelected);
    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context, new Utils().getIdForCategory(categorySelected), notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    SharedPreference pref = new SharedPreference(context);
    pref.saveAlarmCount(categorySelected,1);
    // Cancel alarms
    try {
        alarmManager.cancel(pendingUpdateIntent);
    } catch (Exception e) {

    }
}

My problem is that the pending intent is not canceled after 3 times, which means I get notification each minute. What am I doing wrong?


Solution

  • The actual problem was with my new Utils().getIdForCategory(categorySelected) function which should return a specific value based on the category.

    public int getIdForCategory(String category){
        if (category.equals("A")) return 1; //before it was category=="A"
        if (category.equals("B")) return 2;
        if (category.equals("C")) return 3;
        if (category.equals("D")) return 4;
        if (category.equals("E")) return 5;
        return 0;
    }