问题
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
回答1:
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
来源:https://stackoverflow.com/questions/59725556/when-to-use-jobintentservice-vs-workmanager