AlarmManager - 在创建警报时创建多个重复警报触发器,和

时间:2014-05-30 14:03:05

标签: android alarmmanager

我正在for循环中创建警报。每次我在我的应用程序中创建,删除或更新提醒时,我都会重新运行scheduleAlarms(),以便根据新记录重新创建警报。当我创建一个提醒时,这似乎工作得很好,但当我删除一个时,警报仍然存在。也许有人可以帮我弄清楚为什么在从列表中删除相应的提醒后运行scheduleAlarms()时我的警报没有被删除:

public static void scheduleAlarms(Context context) {

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    // Get all reminders from db and create alarms for them.
    ArrayList<Reminder> reminderList = (ArrayList<Reminder>)DbManager.getInstance().getAllReminders();

    for(Reminder reminder: reminderList) {

        Intent reminderServiceIntent = new Intent(context, ReminderService.class);
        reminderServiceIntent.putExtra(Constants.reminderBundleKey, reminder.getId());

        // Create unique pendingIntent for each reminder, request code is reminderId
        PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);

        // Cancel the alarm before rescheduling
        try {
            alarmManager.cancel(pendingIntent);
        } catch (Exception e) {
            Log.e("rx", "PollReceiver: " + e.toString());
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
        calendar.set(Calendar.MINUTE,reminder.getMinute());

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);

    }

}

/ **更新** / 因此,我不是在每次更新后重新安排所有内容,而是将接收器分成几种方法:

public static void scheduleAlarm(Context context, Reminder reminder) {

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent reminderServiceIntent = new Intent(context, ReminderService.class);
        reminderServiceIntent.putExtra(Constants.reminderBundleKey, reminder.getId());

        // Create unique pendingIntent for each reminder, request code is reminderId
        PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, reminder.getHour());
        calendar.set(Calendar.MINUTE,reminder.getMinute());

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

    }

    public static void cancelAlarm(Context context, Reminder reminder) {

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

            Intent reminderServiceIntent = new Intent(context, ReminderService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context,reminder.getId(),reminderServiceIntent, 0);

            try {
                alarmManager.cancel(pendingIntent);
            } catch (Exception e) {
                Log.e("rx", "PollReceiver: " + e.toString());
            }
    }

0 个答案:

没有答案