已取消的警报显示为已启动

时间:2013-09-02 05:08:41

标签: android android-alarms

我已经阅读了有关如何检查是否存在未决意图警报的stackoverflow问题。

我检查我的闹钟是否存在:

boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
    new Intent("com.my.package.MY_UNIQUE_ACTION"), 
    PendingIntent.FLAG_NO_CREATE) != null);

我设置闹钟并检查它是否存在然后取消闹钟。警报实际上已取消,但如果再次运行上述语句,它仍然返回true。

我已在清单中静态注册了广播接收器。

我设置和取消闹钟的方法如下:

    public static void cancelRecurringAlarm(Context context, String intentAction) {
    Logger.mlog(DEBUG_TAG, "cancelRecurringAlarm called with intent " + intentAction);
    Intent reminder = new Intent();
    reminder.setClass(context, <receiverClassName>);
    reminder.setAction(intentAction);
    PendingIntent recurringReminder = PendingIntent.getBroadcast(context, 0, reminder, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarms.cancel(recurringReminder);
}

public static void setRecurringAlarm(String intentAction, Context context, long timeInMs) {
    Logger.mlog(DEBUG_TAG, "setRecurringAlarm called with intent " + intentAction);

    Intent reminder = new Intent();
    reminder.setClass(context,  <receiverClassName>);
    reminder.setAction(intentAction);
    PendingIntent recurringReminder = PendingIntent.getBroadcast(context, 0, reminder, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarms.setRepeating(AlarmManager.RTC, timeInMs, AlarmManager.INTERVAL_DAY, recurringReminder);
}

1 个答案:

答案 0 :(得分:0)

您将布尔值始终设为 true ,因为您正在取消警报而非待处理的意图。因此取消您在pendingRecurringAlarm中的待处理意图recurringReminder.cancel() )。然后,在取消警报

后,您将获得布尔值 false