Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between timer and alarmmanager

Tags:

java

android

I am a bit confused about Timer and AlarmManager used in Android.

What are the main differences between them?

They are both scheduling a task to run at every A seconds. And what is the main scenario that they are preferred to be used?

For example, for X situation, use Timer but on the other hand, for Y situation, use AlarmManager.

like image 948
mavzey Avatar asked Jan 29 '13 08:01

mavzey


Video Answer


1 Answers

A Timer will start a thread that will keep track of when to start your code. If the device goes asleep, so will the timer thread and your code won't be executed on time. AlarmManager's alarms, on the other hand, are kernel-level. Depending on how you register them, you can request to wake up the device, or execute the next time something wakes up the device. Alarm's are generally preferable and use less resources.

like image 200
Nikolay Elenkov Avatar answered Oct 19 '22 06:10

Nikolay Elenkov