Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Job Scheduler vs Background Service

I have an app which has a feature A which should run in background every minute. Feature A is that the app should connect to a database, read some data then get the current location of the device and based on them check a condition, if the condition is true it should send a statusbar notification to the user so that when the user clicks on the notification the UI of the app will be displayed and something happens.
This background task should run permanently every minute, regardless the app is used, closed, terminated (like facebook or Whatsapp that show us notifications regardless they are in the app stack or not).
Now I have searched and have found that Android offers Job Scheduler,Background Service, AlarmManager and Handlers.
But the more I read about them the more contradictory the statements appear to me.

  1. About Handlers I have read that they do not exist for long delays and will be terminated after system reboot. So they won't be appropriate for my task.
  2. But AlarmManager seems to be a good candidate for the problem because when permitted they exist even after system reboot and can rerun the app. But in the Android Documentation that the Alarm Manager is intended to be used for tasks that have to be run at a specific time (like the Alarm Clock). But my task has to be run every minute.
  3. Then there is Background Service. This is more for tasks like downloading in the background as I have read and not intended for doing something I have explained.
  4. JobScheduler seems not to be for a task that has to be done in permanently, but for tasks that fulfill a specific constraint like idle, or no network... So which of these (or other ones if they exist) do you recommend to use for the task I explained in the first part
like image 733
Code Pope Avatar asked Mar 22 '17 22:03

Code Pope


People also ask

How does a job scheduler work?

Job scheduling is the process where different tasks get executed at pre-determined time or when the right event happens. A job scheduler is a system that can be integrated with other software systems for the purpose of executing or notifying other software components when a pre-determined, scheduled time arrives.

What is the difference between foreground and background service?

For Android Developers, a Service is a component that runs on the background to perform long-running tasks. A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.

What is the difference between job scheduler and WorkManager in Android?

As mentioned in the previous section, WorkManager internally uses the JobScheduler API on Android 6.0 (API Level 23 – Marshmallow) and above devices. Therefore, there won't be any major differences between the WorkManager and JobScheduler on Android 6.0 and above devices.

Why do we need job scheduler?

Managing business processes with a job scheduler streamlines operations and saves time and money. That means that if your competition has an enterprise scheduling solution and you don't, they have a serious resource advantage over you. There's a good chance your competition is already automating.


1 Answers

I have an app which has a feature A which should run in background every minute.

That will not happen on hundreds of millions of Android devices, those running Android 6.0 and higher, due to Doze mode (and, possibly, app standby, depending on the rest of your app).

But AlarmManager seems to be a good candidate for the problem because when permitted they exist even after system reboot

No, they do not. You need to reschedule all alarms scheduled with AlarmManager after a reboot.

the Alarm Manager is intended to be used for tasks that have to be run at a specific time

AlarmManager supports repeating options.

This is more for tasks like downloading in the background as I have read and not intended for doing something I have explained.

A Service will be essential for whatever solution you wind up using.

JobScheduler seems not to be for a task that has to be done in permanently, but for tasks that fulfill a specific constraint like idle, or no network

JobScheduler, as with AlarmManager, supports repeating jobs.

So which of these (or other ones if they exist) do you recommend to use for the task I explained in the first part

Use none of them, as you cannot run things every minute on Android 6.0+ once the device goes into Doze mode, which will be within an hour of the screen turning off. Instead, either redesign the app to only need background work a few times per day, or do not bother writing the app.

like image 192
CommonsWare Avatar answered Sep 22 '22 13:09

CommonsWare