Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use AlarmManager or Handler?

I'm writing an app that constantly polls the device's sensors and every so often should write down some statistics to a file. This could be as fast as once a second or as slow once a minute. Should I use Handler's postDelayed()method or just schedule it with the AlarmManager?

like image 662
Moises Jimenez Avatar asked Nov 05 '12 09:11

Moises Jimenez


People also ask

Is AlarmManager deprecated?

Interface AlarmManager. Deprecated. Interfaces for alarms and alarm management are replaced by ManagedScheduledExecutorService and related interfaces.

What is difference between alarm Manager and Work Manager?

Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management.

What is Android permission SCHEDULE exact alarm?

What is this permission? The new exact alarm permission ( SCHEDULE_EXACT_ALARM ) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.

Why use alarm manager?

By the help of Android AlarmManager in android, you can schedule your application to run at a specific time in the future. It works whether your phone is running or not. The Android AlarmManager holds a CPU wake lock that provides guarantee not to sleep the phone until broadcast is handled.


2 Answers

This should help you discriminate between Handler and AlarmManager. [source]

Though it is agreed these mostly work for API 23. It's a new release.

A flowchart for background work, alarms, and your Android app

like image 119
msysmilu Avatar answered Sep 28 '22 23:09

msysmilu


If the app should work in standby then AlarmManager. If not then Handler.
AlarmManager will wake CPU therefore it will drain battery more, while Handler will not work on standby.

like image 26
pawelzieba Avatar answered Sep 28 '22 22:09

pawelzieba