Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android schedule action

Tags:

android

To make some action for some time I found that there are several choices:

  1. use AlarmManager
  2. use ScheduledExecutorService
  3. use Handler's method postDelayed

What is big difference of all this? What is the best practice of making schedule action?

like image 591
Dmytro Avatar asked Feb 25 '10 11:02

Dmytro


People also ask

Is there a task scheduler for Android?

There are several APIs available to schedule tasks in Android: Alarm Manager. Job Scheduler. GCM Network Manager.

Can you schedule actions in Google home?

Schedule a Routine for when you need it, or start it whenever you want, and Google Assistant can automatically do multiple actions.

Does WorkManager work when app is killed?

Guaranteed: The task runs even if the device restarts. Guaranteed work means that the task will run if the application process is killed or the device is restarted. Thus, an important example for WorkManager would be backing up pictures to a server. This work can be deferred, but it should be guaranteed to run as well.


2 Answers

  1. AlarmManager is the global "Timer", this man can wake your application up, even if it wasn't started. Heavy guy.
  2. ScheduledExecutorService: standard Java way to do some scheduled stuff, used in JSE, simple and familiar for Java developers. The job will be executed in different thread than UI or thread that schedule this job. Well suited for services not to deal with UI and to proccess long and heavy stuff.
  3. Handler: Android way to schedule job, job executes in UI thread (if the handler was created in UI), so it cannot be very heavy proccessing or it'll just freeze your UI.
like image 150
ponkin Avatar answered Oct 22 '22 12:10

ponkin


AlarmManager is independent of your app and guarantees that the task will run.
The other two run as a part of Activity/Service with according life cycle restrictions (e.g. can be killed anytime).

like image 7
yanchenko Avatar answered Oct 22 '22 13:10

yanchenko