Search code examples
androidandroid-serviceandroid-alarms

Adding schedule to android application


I want to create a scheduler for my app. My current idea is to create a background service which starts up on boot and listens to the date and time. This triggers my application. But I'm guessing this isn't the most efficient method. I googled quite extensively and didn't come up with anything different. Is there a more efficient way than creating a service?

[EDIT]

Making my question clearer. I'd like to make something like this.

Say I want to run the app every Monday at 10:00am how would I go about doing that? The method I concocted or something more efficient?


Solution

  • All you need is the system's AlarmManager in conjunction with a BroadcastReceiver which starts an IntentService.

    Update: On second read, what do you mean by trigger my application? I was assuming it's non-interactive, but if it is interactive, you could bring up your Activity with the help of the BroadcastReceiver; no IntentService required then. Saves you the time monitoring either way.

    Update One of the central questions you have to ask yourself is, do I want to wake up the device, such that my {app, service} is run exactly at that point in time, or is it okay to have that run immediately after the device is woken up by the user, if "another Monday 10oo am" has passed by?

    Regardless of the answer to that question, you'll use the system's AlarmManager to set up a repeating alarm, either with setRepeating() or setInexactRepeating(), and with either RTC or RTC_WAKEUP. triggerAtMillis is the time delta to the next Monday 10oo am and intervalMillis is the number of milliseconds which a week has.

    And that's it.