定时器和alarmmanager之间的区别定时器、区别、alarmmanager

2023-09-06 02:10:27 作者:将回忆化作烈酒

我有点困惑定时器 AlarmManager 在Android中使用。

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

什么是他们的之间的主要区别是什么?

What are the main differences between them?

他们都安排任务在每次秒运行。什么是主要的情况下,他们将要使用preferred?

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?

例如,对于x的情况下,使用定时器但另一方面,Y的情况下,使用 AlarmManager

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

推荐答案

A 定时器将启动一个线程,该线程将跟踪何时开始code的。如果该设备便睡着了,于是将计时器线程和你的code将不能按时执行。 AlarmManager 的报警,在另一方面,是内核级。取决于你如何注册它们,你可以要求唤醒设备,或执行下一次的东西唤醒设备。报警的普遍preferable并使用更少的资源。

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.