Kurlicue Kurlicue - 2 years ago 141
Android Question

AlarmManager vs Handler/Timer with long timer

I'm designing an app that will contain a schedule, when the user adds an event to it he can define if it repeats "every week", "every 2 weeks" etc, if it's every week it's fine but when it's, lets say "once a month", I have to calculate the time since the user added that event and remove the event a week after, and then add it back in 3 weeks.

From my research I can use an AlarmManager, a Handler or a Timer. It must update while the app is not running but it doesn't need to be while the phone is asleep, I feel like an AlarmManager would be an overkill for this simple task, but using a Timer or a Handler to calculate the time since the user added an event I would need to to something like

(time passed since the user added the event) + (time the phone was asleep)
and I'm not sure how to do that or if it's even possible

AlarmManager, Timer, Handler or other?

Answer Source

For your case, you should definitely use AlarmManager.

Running a Handler along with a Timer should only be used for short periods of time. So that you are sure the app is still running.

AlarmManager on the other hand should be used to perform operations outside the lifetime of your application, which is the case in your app.

Refer to the following note copied from the training docs

Note: For timing operations that are guaranteed to occur during the lifetime of your application, instead consider using the Handler class in conjunction with Timer and Thread. This approach gives Android better control over system resources.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download