0

I have a working alarm using the alarmManager class. If I set the alarm for any time before midnight of the current day everything is fine. If I want to set the alarm for the 7 a.m., however, and 7 a.m. has already come and gone for today, that of course does not work.

Is there a way to do this without implementing datepicker and dates into the program?

Below is a code sample. I can post more complete code if needed.

 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();

        }
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
  • Just set tomorrow's date on your `calendar` object. – Mike M. Oct 09 '15 at 22:05
  • Hello Mike, I am looking to have the alarm go off the next instance the clock time set occurs. If i set the date for tomorrow automatically, that might be unexpected behavior for someone that is setting the alarm for later today. Using the datepicker for the alarm may be unavoidable then. The default GUI for the timepicker and datepicker aren't visually appealing, especially if you are trying to put both on the same screen. With even standard display sizes for phones, the datepicker and timepicker take up almost all of the screen. – joshgoldeneagle Oct 12 '15 at 18:20
  • As I understand it, you want your app to function like a regular alarm clock - just set the time and am/pm. If so, then your app will just have to determine if the time set has already passed for today. If it has, then you would set `calendar` ahead a day. – Mike M. Oct 13 '15 at 05:20
  • Mike, that sounds like a straightforward solution, thank you! – joshgoldeneagle Oct 13 '15 at 16:41
  • No problem. Just wanted to mention that the `Calendar` class offers the `after()`, `before()`, and `compareTo()` methods, and that `Calendar.getInstance()` returns a calendar with the current date/time, so it's pretty simple to determine if the alarm time has passed for today. – Mike M. Oct 13 '15 at 16:54
  • Mike, see answer I posted... also I refactored Calendar object calendar to alarmTime. Thx – joshgoldeneagle Oct 13 '15 at 19:12
  • Looks good. I think she'll work. Cheers! – Mike M. Oct 13 '15 at 19:41

1 Answers1

0

I am testing this solution as recommended by Mike M. I will know tomorrow morning if this works. Mike if you want to post the same solution separately, and this is successful (and I think it will be), I can award you points.

 //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);
           }
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30