如何取消所有待处理的警报

时间:2014-05-29 15:20:58

标签: android

在我的应用中,我想让用户删除他们设置的闹钟。

以下是我设置闹钟的方法:

    ArrayList<Long> alarmDates = DateAndTimeAssistant.turnReminderTimesIntoActualDates(reminderHolder);
    alarms = new ArrayList<Alarm>();
    for (long alarmDate : alarmDates) {
        Alarm alarm = new Alarm();
        alarm.setTimeInMiliseconds(alarmDate);
        alarm.setReminderHolderId(reminderHolder);
        alarm.setTitle("title");
        alarm.setDescription("description");
        alarms .add(alarm);
    }

然后我将它们插入到DB和onSuccess中,我这样做:

DateAndTimeAssistant.addAlarmsToPushNotificationService(alarms);

以下是发生的事情:

for (Alarm alarm : alarms) {
        Calendar alarmCalendar = Calendar.getInstance();
        alarmCalendar.setTimeInMillis(alarm.getTimeInMiliseconds());

        Intent intent = new Intent(context, PushNotificationReceiver.class);
        intent.putExtra("notificationTitle", alarm.getTitle());
        intent.putExtra("notificationText", alarm.getDescription());


        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) alarmCalendar.getTimeInMillis(), intent, 0); // The second arg is unique id

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // Service.ALARM_SERVICE
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
    }

现在,在与开始这一切的活动完全不同的活动中,我想删除警报。

我在一篇文章中找到了以下代码:

        for (long alarmTime : alarms) { // This is the unique ID of the pendingIntent I created earlier
            PendingIntent pIntent = PendingIntent.getBroadcast(a, (int) alarmTime, intent, PendingIntent.FLAG_CANCEL_CURRENT);

但我不知道这里的“意图”会传递什么?

编辑:在我得到第一个答案后,我改变了代码:

Intent intent = new Intent(context, PushNotificationReceiver.class);

        for (long alarmTime : alarms) { // This is the unique ID of the pendingIntent I created earlier
            PendingIntent pIntent = PendingIntent.getBroadcast(a, (int) alarmTime, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

但是在假定的删除之后警报仍然会停止。还有什么可能出错? 我现在意识到我没有对这个PendingIntent Im创造任何事情。我该怎么办呢?

编辑2:
现在我添加了一个警报管理器,我将待处理的意图传递给它,然后取消它们,但它仍然无法正常工作

        AlarmManager alarmManager = (AlarmManager) a.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(a, PushNotificationReceiver.class);
        for (long alarmTime : alarms) {
            PendingIntent pIntent = PendingIntent.getBroadcast(a, (int) alarmTime, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            // Cancel alarms
            try {
                alarmManager.cancel(pIntent);
            } catch (Exception e) {
               // Cant do anything about it.
            }
        }

3 个答案:

答案 0 :(得分:3)

您需要创建与创建警报时使用的Intent相同的Intent intent = new Intent(context, PushNotificationReceiver.class); 。请注意,在比较Intents时,会忽略额外内容,因此您只需要

{{1}}

答案 1 :(得分:0)

您可以使用以下方法取消报警

 public void cancelAlarm(Context context, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Cancel Alarm using Reminder ID
        mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
        mAlarmManager.cancel(mPendingIntent);

        // Disable alarm
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

答案 2 :(得分:0)

我也有同样的问题。我不得不取消之前设置的所有待处理警报。

我使用相同的requestId创建了待处理的意图(我在创建警报时将其保存到数据库,然后检索它),然后取消它。

https://stackoverflow.com/a/46278582/4432176