Search code examples
androidalarmmanagercountdowntimer

Should I use AlarmManager or CountDownTimer for my application?


So I'm new to android and working on an parking alert application which takes time input as variable to initiate a countdown timer that will trigger an alarm/notification/sound before the countdown timer reach 00:00(example : 02:00 remaining). The problem is after the countdown timer is initiate, the application will eventually goes background (closed) and wait for the alarm/alert to be triggered.

I have gone through AlarmManager documentation and known that it allows the countdown timer to continue running even the application is closed.

But for CountDownTimer , can the countdown timer keep running in background and triggered after the application is closed?

Which 1 I should be using in this case?AlarmManager or CountDownTimer ? Thanks and appreciate for any helps!

EDIT : I need to use both for my case.


Solution

  • You should use the AlarmManager.

    Why: The AlarmManager can start a background service to run your alarm code in. This works even if your app is killed. The CountDownTimer won't do this. It simply stops when someone exits your your app.

    The AlarmManager can also wake your device up if it's in a deep sleep mode. This is very useful because most phones enter deep sleep when the screen turns off.