无法取消警报管理器

Cannot cancel alarmmanager

我试图在从日期选择器和时间选择器中选择所需的日期和时间后创建 3 个通知。我想针对所选值发送最近 3 天的通知。但是在 3 个通知之后我想取消通知过程。我从 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
        }
    }
};

我的接收器 class 是:

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);
    }
}

还有我的清除方法

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) {

    }
}

我的问题是挂起的意图在 3 次后没有被取消,这意味着我每分钟都会收到通知。我做错了什么?

实际问题出在我的 new Utils().getIdForCategory(categorySelected) 函数上,它应该 return 基于类别的特定值。

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;
}