使用AlarmManager类的警报仅适用于当天

时间:2015-10-09 21:50:32

标签: android alarmmanager

我使用alarmManager类进行了工作报警。如果我在当天午夜之前的任何时间设置闹钟,一切都很好。如果我想在早上7点设置闹钟,但是今天上午7点已经过去了,那当然不起作用。

如果没有在程序中实现datepicker和日期,有没有办法做到这一点?

以下是代码示例。如果需要,我可以发布更完整的代码。

 Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);

        repeatInterval = LoadPreferences("repeatInterval", repeatInterval);  //gets number of minutes reminder should repeat

        repeatIntervalMilliseconds = repeatInterval * 1000 * 60;  //converts repeating interval to milliseconds for setRepeating method

        //Set a one time alarm
        if (repeatInterval == 0) {
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
            AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined

            Toast.makeText(AlarmActivity.this, "Your one time reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel, Toast
                    .LENGTH_LONG)
                    .show();
        }

        //Set a repeating alarm
        else {
            alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), repeatIntervalMilliseconds, pendingIntent);
            AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined

                Toast.makeText(AlarmActivity.this, "Your reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel + " and will " +
                        "repeat " +
                        "every " +
                        repeatInterval + " minutes.", Toast.LENGTH_LONG).show();

        }

1 个答案:

答案 0 :(得分:0)

我正在按照Mike M.的推荐测试这个解决方案。如果有效,我明天早上会知道。迈克,如果你想分别发布相同的解决方案,这是成功的(我认为会是),我可以奖励你积分。

 //Add 1 day to alarmTime if alarmTime has already gone by for today
           calendarComparison = now.compareTo(alarmTime);
           if (calendarComparison == 1){
               alarmTime.add(Calendar.DATE, 1);
           }