Since Android Oreo background execution limits, the docs recommend to refactor IntentService
s to JobIntentService
.
https://developer.android.com/about/versions/oreo/background
JobIntentService
runs immediately as an IntentService
below Oreo, but schedules a Job on Oreo+
https://developer.android.com/reference/android/support/v4/app/JobIntentService
In what cases would it make sense to run a normal IntentService
as a foreground Service
with a persistent notification, and when is a JobIntentService
better?
One downside I can see in JobIntentService
is that it doesn't start immediately.
Both work same but the only difference with JobIntentService is that JobIntentService gets restarted if the application gets killed while the service was executing.
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.
You can say JobIntentService is a modern way to run the background service from the background application. JobIntentService works in the same way as a Service however it enqueues the work into the JobScheduler on compatible Android targets( SDK 26 or more).
Limitations / Drawbacks The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
If you some limited amount of tasks to be performed in the background, then you can use Service, otherwise, you can use IntentService. You can refer to the Android official documentation for the best practice on Background Services. Team MindOrks!
You can use JobIntentService as a replacement for IntentService that is compatible with newer versions of Android. Deprecating the IntentService class will force developers to write error prone custom threaded foreground Services if they need immediate work done that won't die.
If you some limited amount of tasks to be performed in the background, then you can use Service, otherwise, you can use IntentService. You can refer to the Android official documentation for the best practice on Background Services.
To start a Service, use the onStartService () function, but to start an IntentService, use Intent, i.e. start the IntentService by calling Context.startService (Intent). Because Service operates on the Main thread, there is a risk that your Main thread will be stopped if you utilise it.
Foreground service is not affected by Doze, but you still have to use wake locks, if you need your task to be continued when the screen is off.
The JobIntentService (which uses the JobScheduler) manages wake locks for you, but you have less control when the job will be started.
I would use the foreground IntentService (or Service) for high priority tasks (e.g. downloading a database) that should run immediatelly and that should not be paused / killed by system.
I would use the JobIntentService in conjunction with AlarmManager to schedule low priority tasks like refreshing the widget's data periodically.
If you want to make long running operation something like Music Player use Foreground Service with notification. because JobIntentService has time execution limit like JobScheduler. ( 10 minutes)
If you think that user don't need to know about your work you can use JobIntentService without notification.
You don't need to worry about Doze mode if user is actively using your app.
There are some cases for Doze mode, according to https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/
Light-Doze starts to kick in shortly after both the screen is off and the device is not charging, waiting only a couple minutes before applying the restrictions just to make sure the user has stopped using their phone
For example, user turned screen off while you are computing something. Your task is finished and you want to run background service. In that case your JobIntentService can be deffered, because device can be in doze mode.
However, if you want immediately perform background operation use ForegrounService with WakeLock, because ForegroundService is not working when the screen is off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With