JobService


public abstract class JobService
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.app.job.JobService


Entry point for the callback from the JobScheduler.

This is the base class that handles asynchronous requests that were previously scheduled. You are responsible for overriding JobService.onStartJob(JobParameters), which is where you will implement your job logic.

This service executes each incoming job on a Handler running on your application's main thread. This means that you must offload your execution logic to another thread/handler/AsyncTask of your choosing. Not doing so will result in blocking any future callbacks from the JobScheduler - specifically onStopJob(android.app.job.JobParameters), which is meant to inform you that the scheduling requirements are no longer being met.

Since the introduction of JobScheduler, if an app did not return from onStartJob(android.app.job.JobParameters) within several seconds, JobScheduler would consider the app unresponsive and clean up job execution. In such cases, the app was no longer considered to be running a job and therefore did not have any of the job lifecycle guarantees outlined in JobScheduler. However, prior to Android version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, the failure and cleanup were silent and apps had no indication that they no longer had job lifecycle guarantees. Starting with Android version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, JobScheduler will explicitly trigger an ANR in such cases so that apps and developers can be aware of the issue. Similar behavior applies to the return time from onStopJob(android.app.job.JobParameters) as well.

If you see ANRs, then the app may be doing too much work on the UI thread. Ensure that potentially long operations are moved to a worker thread.

As a subclass of Service, there will only be one active instance of any JobService subclasses, regardless of job ID. This means that if you schedule multiple jobs with different job IDs but using the same JobService class, that JobService may receive multiple calls to onStartJob(android.app.job.JobParameters) and onStopJob(android.app.job.JobParameters), with each call being for the separate jobs.

Summary

Constants

int JOB_END_NOTIFICATION_POLICY_DETACH

Detach the notification supplied to setNotification(android.app.job.JobParameters, int, android.app.Notification, int) when the job ends.

int JOB_END_NOTIFICATION_POLICY_REMOVE

Cancel and remove the notification supplied to setNotification(android.app.job.JobParameters, int, android.app.Notification, int) when the job ends.

String PERMISSION_BIND

Job services must be protected with this permission:

     <service android:name="MyJobService"
              android:permission="android.permission.BIND_JOB_SERVICE" >
         ...
          
    

Inherited constants

Public constructors

JobService()

Public methods

final void jobFinished(JobParameters params, boolean wantsReschedule)

Call this to inform the JobScheduler that the job has finished its work.

void onNetworkChanged(JobParameters params)

This method is called that for a job that has a network constraint when the network to be used by the job changes.

abstract boolean onStartJob(JobParameters params)

Called to indicate that the job has begun executing.

abstract boolean onStopJob(JobParameters params)

This method is called if the system has determined that you must stop execution of your job even before you've had a chance to call jobFinished(android.app.job.JobParameters, boolean).

final void setNotification(JobParameters params, int notificationId, Notification notification, int jobEndNotificationPolicy)

Provide JobScheduler with a notification to post and tie to this job's lifecycle.

final void updateEstimatedNetworkBytes(JobParameters params, JobWorkItem jobWorkItem, long downloadBytes, long uploadBytes)

Update the amount of data this JobWorkItem is estimated to transfer after the job has started.

final void updateEstimatedNetworkBytes(JobParameters params, long downloadBytes, long uploadBytes)

Update the amount of data this job is estimated to transfer after the job has started.

final void updateTransferredNetworkBytes(JobParameters params, long transferredDownloadBytes, long transferredUploadBytes)

Tell JobScheduler how much data has successfully been transferred for the data transfer job.

final void updateTransferredNetworkBytes(JobParameters params, JobWorkItem item, long transferredDownloadBytes, long transferredUploadBytes)

Tell JobScheduler how much data has been transferred for the data transfer JobWorkItem.

Inherited methods