0

我正在创建一个选择了某些特定日期的警报。我希望在那些工作日的某个时间点重复警报。But the alarm is fired as soon as i create an alarm, when past and future week days are selected.

例如,将今天视为“星期三”,如果我选择星期二和星期四,则会在创建它时触发警报。但它应该分别在即将到来的星期二和明天触发。我无法在以下代码中找到错误。

我找不到我在做什么错误。

//scheduling the alarm for repeating on selected days
public void scheduleDay(int request_code_value, String alarm_title) 
{

   Calendar calendar = Calendar.getInstance();


   for(int i = 1; days.size() >= i; i++)
   {
       if ((days.get("" + i)) == 1)
       {
           // for creating different alarms adding i to request_code_value  for uniqueness
           request_code_value = request_code_value +i;

           String filter_action = "com.ibkr.yowakeup" + request_code_value +"_time";


            IntentFilter filter = new IntentFilter(filter_action);

            registerReceiver(new AlarmReciever(), filter);


            Intent intent = new Intent(filter_action);
            intent.putExtra(getString(R.string.get_current_intent_value), request_code_value);
            intent.putExtra(getString(R.string.alarmtext), alarm_title);
            intent.putExtra(getString(R.string.alarm_time), newAlarm_Choose_Alarm_Value.getText().toString());


            Log.d(TAG,"Scheduled on " + i + " = " + days.get("" + i));

            calendar.set(Calendar.DAY_OF_WEEK, i);
            calendar.set(Calendar.HOUR_OF_DAY, selected_hour);// cal.get(Calendar.HOUR_OF_DAY);
            calendar.set(Calendar.MINUTE, selected_min);// cal.get(Calendar.MINUTE);
            calendar.set(Calendar.SECOND, 0);// cal.get(Calendar.SECOND);
            calendar.set(Calendar.MILLISECOND, 0);


            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code_value , intent, 0);

            alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
                    AlarmManager.INTERVAL_DAY * 7,
                    pendingIntent);

        }
   }

}

请帮我。提前致谢。

4

1 回答 1

0

您正在尝试通过操作来设置日历DAY_OF_WEEK。一个Calendar实例总是代表一个特定的时间点。当您调用时,Calendar.getInstance()您将获得一个Calendar初始化为当前日期和时间的实例。让我们举个例子:

我今天在 2015 年 3 月 8 日星期日 08:00 运行此代码。Calendar我从通话中获得的信息Calendar.getInstance()被初始化为此日期/时间。由于今天是星期天,DAY_OF_WEEK如果你跟注,你将得到calendar.get(Calender.DAY_OF_WEEK)的值Calendar.SUNDAY是 1。

假设我现在想为星期二设置闹钟。在这种情况下,您的代码将调用:

calendar.set(Calendar.DAY_OF_WEEK, i);

wherei设置为Calendar.TUESDAYwhich 的值为 3。

现在应该怎么办?对于日期/时间的所有这些部分,该Calendar实例实际上没有单独的字段。它仅包含一个表示特定日期/时间的值。DAY_OF_WEEK否则,如果您尝试将 设置为SUNDAY并将 设置DAY_OF_MONTH为 9(3 月是星期一),则可能会产生不一致。此外,没有办法告诉它Calendar是否应该将日期调整为上一个SUNDAY或下一个SUNDAY。可能有一种方法可以确定这一点,但我不会这样做。

出于这个原因,你不应该尝试设置 CalendarDAY_OF_WEEK. 相反,您应该通过调用来确定当前的星期几calendar.get(Calendar.DAY_OF_WEEK),然后通过添加正确的天数来向前调整日历实例以到达您想要的星期几。像这样:

int desiredDay = // the day of the week you want to set the alarm for
int today = calendar.get(Calendar.DAY_OF_WEEK);
int numberOfDaysToAdd = desiredDay - today;
if (desiredDay < today) {
    // Desired day is earlier in the week than today, add 7 days to
    //  ensure it is in the future
    numberOfDaysToAdd += 7;
}
calendar.add(Calendar.DAY, numberOfDaysToAdd);

您可能仍需要检查您的闹钟所需时间是否已经过去,并使用类似的算法将其设置为将来的日期。希望你明白这一点。

于 2015-03-08T17:30:06.367 回答