1

I'm trying to call a method on a specified time by the user. For example, "at 4:00pm- send a message to Bob".

I understand how to get the calendar.HOUR(), calendar.MINUTE(), etc; however, I'm unsure where to place this code. I want to make sure the method calls on the right time, but I also don't want to have to update the calendar time every second (for fear of too much battery usage of RAM).

I'm currently using this, which is in the onCreate method.

    if (calendarHour == startHour && calendarMinute == startMinute)
{
//do stuff
}

Is there a standard on how to check what time is it without spamming? What code would I use and where do I place it?

  • 2
    this might be of use to you - http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android – radai Apr 20 '13 at 19:59

1 Answers1

3

Use AlarmManager.

Calendar cal; // set your desired time
PendingIntent operation = PendingIntent.getService( /*  register a service that "sends a message to Bob"*/ );

final AlarmManager am = context.getSystemService(Context.ALARM_SERVICE).
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), operation);
// you can close your app now.
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99