To make some action for some time I found that there are several choices:
- use AlarmManager
- use ScheduledExecutorService
- use Handler's method postDelayed
What is big difference of all this? What is the best practice of making schedule action?
To make some action for some time I found that there are several choices:
What is big difference of all this? What is the best practice of making schedule action?
AlarmManager
is the global "Timer", this man can wake your application up, even if it wasn't started. Heavy guy.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.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.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).