JobIntentService
abstract class JobIntentService : Service
kotlin.Any | ||||
↳ | android.content.Context | |||
↳ | android.content.ContextWrapper | |||
↳ | android.app.Service | |||
↳ | androidx.core.app.JobIntentService |
Helper for processing work that has been enqueued for a job/service. When running on Android O
or later, the work will be dispatched as a job via JobScheduler.enqueue
. When running on older versions of the platform, it will use Context.startService
.
You must publish your subclass in your manifest for the system to interact with. This should be published as a android.app.job.JobService
, as described for that class, since on O and later platforms it will be executed that way.
Use enqueueWork(Context, Class, int, Intent)
to enqueue new work to be dispatched to and handled by your service. It will be executed in onHandleWork(Intent)
.
You do not need to use androidx.legacy.content.WakefulBroadcastReceiver
when using this class. When running on Android O
, the JobScheduler will take care of wake locks for you (holding a wake lock from the time you enqueue work until the job has been dispatched and while it is running). When running on previous versions of the platform, this wake lock handling is emulated in the class here by directly calling the PowerManager; this means the application must request the android.Manifest.permission#WAKE_LOCK
permission.
There are a few important differences in behavior when running on Android O
or later as a Job vs. pre-O:
- When running as a pre-O service, the act of enqueueing work will generally start the service immediately, regardless of whether the device is dozing or in other conditions. When running as a Job, it will be subject to standard JobScheduler policies for a Job with a
android.app.job.JobInfo.Builder#setOverrideDeadline(long)
of 0: the job will not run while the device is dozing, it may get delayed more than a service if the device is under strong memory pressure with lots of demand to run jobs. - When running as a pre-O service, the normal service execution semantics apply: the service can run indefinitely, though the longer it runs the more likely the system will be to outright kill its process, and under memory pressure one should expect the process to be killed even of recently started services. When running as a Job, the typical
android.app.job.JobService
execution time limit will apply, after which the job will be stopped (cleanly, not by killing the process) and rescheduled to continue its execution later. Job are generally not killed when the system is under memory pressure, since the number of concurrent jobs is adjusted based on the memory state of the device.
Here is an example implementation of this class:
Summary
Public constructors | |
---|---|
<init>() Default empty constructor. |
Public methods | |
---|---|
open static Unit |
enqueueWork(@NonNull context: Context, @NonNull cls: Class<*>, jobId: Int, @NonNull work: Intent) Call this to enqueue work for your subclass of |
open static Unit |
enqueueWork(@NonNull context: Context, @NonNull component: ComponentName, jobId: Int, @NonNull work: Intent) Like |
open Boolean |
Returns true if |
open IBinder? |
Returns the IBinder for the |
open Unit |
onCreate() |
open Unit | |
open Int |
onStartCommand(@Nullable intent: Intent?, flags: Int, startId: Int) Processes start commands when running as a pre-O service, enqueueing them to be later dispatched in |
open Boolean |
This will be called if the JobScheduler has decided to stop this job. |
open Unit |
setInterruptIfStopped(interruptIfStopped: Boolean) Control whether code executing in |
Protected methods | |
---|---|
abstract Unit |
onHandleWork(@NonNull intent: Intent) Called serially for each work dispatched to and processed by the service. |
Public constructors
<init>
JobIntentService()
Default empty constructor.
Public methods
enqueueWork
open static fun enqueueWork(
@NonNull context: Context,
@NonNull cls: Class<*>,
jobId: Int,
@NonNull work: Intent
): Unit
Call this to enqueue work for your subclass of JobIntentService
. This will either directly start the service (when running on pre-O platforms) or enqueue work for it as a job (when running on O and later). In either case, a wake lock will be held for you to ensure you continue running. The work you enqueue will ultimately appear at onHandleWork(Intent)
.
Parameters | |
---|---|
context |
Context: Context this is being called from. |
cls |
Class<*>: The concrete class the work should be dispatched to (this is the class that is published in your manifest). |
jobId |
Int: A unique job ID for scheduling; must be the same value for all work enqueued for the same class. |
work |
Intent: The Intent of work to enqueue. |
enqueueWork
open static fun enqueueWork(
@NonNull context: Context,
@NonNull component: ComponentName,