Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use JobIntentService vs WorkManager?

Google recently deprecated IntentService as of: https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029932dfd24945202017639754b00acc2e

The docs for IntentService now say:

 * @deprecated IntentService is subject to all the  *   <a href="/preview/features/background.html">background execution limits</a>  *   imposed with Android 8.0 (API level 26). Consider using {@link androidx.work.WorkManager}  *   or {@link androidx.core.app.JobIntentService}, which uses jobs  *   instead of services when running on Android 8.0 or higher. 

So what are the differences between JobIntentService and WorkManager and which one is recommended under which circumstances?

Google doesn't even mention JobIntentService on this page, they only mention WorkManager: https://developer.android.com/guide/background

like image 545
satur9nine Avatar asked Jan 13 '20 23:01

satur9nine


People also ask

When would you use a WorkManager?

Use WorkManager for reliable workWorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

Why is JobIntentService deprecated?

This class is deprecated. This class has been deprecated in favor of the Android Jetpack WorkManagerlibrary, which makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts.

Is WorkManager deprecated?

This method is deprecated.

What is difference between WorkManager and service?

WorkManager provides a better way of handling background tasks. Runs on devices with/without Google play services. No need to worry about threading as WorkManager will ensure execution on the background thread. Easy to schedule, cancel, retry & query the work.


1 Answers

Since Android Oreo we cannot keep normal Services running on the background anymore, because the system will:

1-kill the service after around one minute if the app itself goes to the background after launching the service

2-throw an exception if the service was launched when the app itself is in the background

IntentService is just a subclass of the normal service, which executes all its work sequentially on a background thread and stops itself when it finishes executing all its work. But as a service, it is, as well, affected by the limitations mentioned above.

Now for the JobIntentService:

Will act as a normal IntentService on pre-Oreo Devices (because we don't have any limitations) and on Oreo+ will use jobScheduler instead to achieve similar behavior as IntentService. It just starts the work as soon as possible, schedules its work via JobScheduler, and JobScheduler may elect to postpone that work for a bit, but its job are more likely to be differed or interrupted in low-memory situtations ,in doze mode or when they reach a time limit(~10 minutes)

With JobIntentService, doing some configurations is impossible, like defining specifically under which circumstances we want our jobs to start (such as when the device is currently Charging or if we have WIFI connection), however with workmanager we can set these constraints.

Use WorkManager for jobs that have some Constraints,or for jobs/work that are transactional not ongoing, or for jobs that can happen sometime in the future, and use JobIntentService when you want to copy the behavior of the normal IntentService on Android Oreo+ and for jobs that can be slightly delayed and might take more than 1 minute but less than 10 minutes approx.

Hope I Answered Your question.

Regards

like image 154
Hady Salhab Avatar answered Sep 18 '22 07:09

Hady Salhab