I have an app that uses a CountDownTimer
inside a BraodcastReceiver
. The CountDownTimer
can be for upwards of 1 hour. The timer shows the countdown in the Notifications area (second intervals).
Some users have reported that the app seems to hang on long count downs. The CountDownTimer is triggered by a widget.
Does anyone know if a CountDownTimer
can be stopped and reclaimed by the OS?
The alternative would be to set a recurring alarm at 1 second intervals which runs a service. Is there a better option?
Does anyone know if a CountDownTimer can be stopped and reclaimed by the OS?
Your process will be.
The alternative would be to set a recurring alarm at 1 second intervals which runs a service.
That's not an option in any practical sense, if by "recurring alarm" you mean AlarmManager
. AlarmManager
is not designed for every-second events.
Is there a better option?
This is one of the few cases that justifies a foreground service. Since you have a Notification
anyway, and since your AlarmManager
approach would keep the service around constantly anyway, you may as well dispense with the AlarmManager
and use startForeground()
to keep the service around. Update the Notification
that you are using with startForeground()
, and use ScheduledExecutorService
to get control every second on a background thread.
When the countdown is done, call stopForeground()
and stopSelf()
to get rid of it all.