Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CountDownTimer vs AlarmManager

Are there any major differences between CountDownTimer and AlarmManager? I don't mean syntax or how to use it but wondering if there are such tasks when you certainly can say that I should use one of them, not another?
Simple example, I have to launch some action once a minute. What should I use? CountDownTimer or AlarmManager? How it depends?

like image 946
Vitalii Korsakov Avatar asked Oct 06 '22 09:10

Vitalii Korsakov


1 Answers

AlarmManager generally is used for purposes where your application is closed or must execute some repeating task(s). It is also slightly less precise than Timer or Handler.

CountDownTimer is used more for running a specific task at a duration (onFinish), and being updated periodically until it executes (onTick).

In your specific case, I believe you should be using AlarmManager, since it can repeat indefinitely. If you want something to execute every second for 10 seconds, for example, use CountDownTimer.

like image 141
Cat Avatar answered Oct 10 '22 02:10

Cat