JobIntentService – Android Studio Tutorial
In this video, we will learn how to use the JobIntentService class in Android.
The JobIntentService combines 2 different types of services: the IntentService and the JobService.
Since Android Oreo (API 26), background services can’t keep running while the app itself is in the background. Instead, the system will kill them after around 1 minute or throw an IllegalStateException if we try to call startService from the background. Since IntentService is a subclass of Service, it is affected by these background execution limits.
The recommended alternative for the IntentService is the JobIntentService, which starts a normal IntentService on API 25 and lower, but on Android Oreo and higher it uses the JobScheduler to schedule a JobService with setOverrideDeadline(0) instead.
The same as in the IntentService’s onHandleIntent method, all the incoming intents are executed sequentially on a background thread in the JobIntentService’s onHandleWork method, and we can send data to it in form of intent extras. When the service is first created, it runs through onCreate, and when it finishes executing all the work, it automatically stops itself and onDestroy is called. We don’t have to acquire a wake lock manually in the JobIntentService, since the superclass takes care of this. For this reason, we have to add the WAKE_LOCK permission into the manifest file.
JobServices are more likely to get deferred or interrupted under memory pressure, in doze mode or when they reach a time limit, but the system can restart them at a later time. In onStopCurrentWork, we can decide if we want to cancel a job and drop the current and all following intents if the system interrupts it, or if we want to reschedule the job with the last intent. In both cases, we should stop the currently running work by checking if isStopped returns true, because otherwise, the system will ultimately kill the service.
We start the JobIntentService with the static enqueueWork method, where we have to pass a context, the JobIntentService class we want to start, a unique jobId and the service intent. The system will then, depending on the API level, either start an IntentService immediately, or use the JobScheduler to enqueue a job, which requires the BIND_JOB_SERVICE permission, that we have to add to the service tag in the AndroidManifest.xml file.
Example code:
https://gist.github.com/codinginflow/758ec7a1a011db5cdc6a7cbe81084436
____________________
💻 Find the BEST programming tutorials on TutHub:
https://tuthub.io
⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching
❗ Subscribe to the channel:
https://www.youtube.com/c/codinginflo…
📨 Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter
❓ Join our free developer community:
https://discord.gg/TSnMvmc
📣 Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
TikTok: https://www.tiktok.com/@codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow
💰 Business requests, sponsoring, etc.: info@codinginflow.com
Views :37602
android studio