报警管理器:重复报警并不总是触发

时间:2015-05-19 18:47:54

标签: java android alarmmanager alarm android-alarms

我正在尝试开发一种提醒系统,因此用户可以输入提醒日期(MON,TUE ...)和此提醒将触发的时间(仅限小时和分钟)。它可以是一周中的任何一天或一周中的多天。以下是设置这些提醒的代码:

        final AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        for (final Day day: reminder.getDays()) {
            for (final ReminderTime reminderTime: reminder.getTimes()) {
                final Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, reminderTime.getHour());
                calendar.set(Calendar.MINUTE, reminderTime.getMinute());
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.DAY_OF_WEEK, day.getCalendarDay());

                if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
                    // final long daysDifference = DateUtils.getDaysDifference(calendar.getTimeInMillis(), System.currentTimeMillis());
                    // calendar.add(Calendar.DAY_OF_YEAR, (int) (daysDifference + 1));
                    calendar.add(Calendar.DAY_OF_YEAR, 7);
                }

                final Intent intent = createReminderIntent(context, reminder.getReminderType(), reminderTime, day);
                final PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, sender);
            }
        }

我稍后在自定义BroadcastReceiver中收到的警报。 问题是提醒是不时发射的,有时候并不是,在我看来,有些事情正在酝酿着日子。我想知道是错误还是我做错了什么?

2 个答案:

答案 0 :(得分:1)

使用相同的PendingIntent.get*(...)和请求代码调用Intent时,会返回PendingIntent的同一个实例。

AlarmManager只能为一个PendingIntent关联一个规则,这意味着代码中只有AlarmManager.setRepeating(...)设置的最后一个警报实际上处于活动状态。其他人被这最后一条规则所覆盖。

区分PendingIntent与同一Intent的一种方法是为每个AlarmManager使用不同的请求代码。当传递给Intent时,这些将按预期触发单个警报。

可悲的是,没有办法取消定义的多个警报,例如基数为PendingIntent所以你必须

  • 保留最初用于安排闹钟的所有PendingIntent个实例
  • 保留所有请求代码并重新构建所述AlarmManager.cancel(pi) s
  • 或使用类似的机制。

然后,您需要分别使用这些PendingIntent中的每个tvs[i]来取消关联的警报。

答案 1 :(得分:0)

Calendar Calendar_Object = Calendar.getInstance();

    Calendar_Object.set(Calendar.DAY_OF_WEEK, 6);


    Calendar_Object.setTimeInMillis(System.currentTimeMillis());
    Calendar_Object.set(Calendar.HOUR_OF_DAY, 16);
    Calendar_Object.set(Calendar.MINUTE, 51);
    Calendar_Object.set(Calendar.SECOND, 0);

    // MyView is my current Activity, and AlarmReceiver is the
    // BoradCastReceiver
    Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
            0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    /*
     * The following sets the Alarm in the specific time by getting the long
     * value of the alarm date time which is in calendar object by calling
     * the getTimeInMillis(). Since Alarm supports only long value , we're
     * using this method.
     */

      Long alarmTime = Calendar_Object.getTimeInMillis();

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7 * 24 * 60 * 60 * 1000 , pendingIntent);