Skip to content

Most visited

Recently visited

navigation

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 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 JobManager - specifically onStopJob(android.app.job.JobParameters), which is meant to inform you that the scheduling requirements are no longer being met.

Summary

Constants

String PERMISSION_BIND

Job services must be protected with this permission:

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

Inherited constants

From class android.app.Service
From class android.content.Context
From interface android.content.ComponentCallbacks2

Public constructors

JobService()

Public methods

final void jobFinished(JobParameters params, boolean needsReschedule)

Call this to inform the JobManager you've finished executing.

abstract boolean onStartJob(JobParameters params)

Override this method with the callback logic for your job.

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(JobParameters, boolean).

Inherited methods

From class android.app.Service
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.content.ComponentCallbacks2
From interface android.content.ComponentCallbacks

Constants

PERMISSION_BIND

added in API level 21
String PERMISSION_BIND

Job services must be protected with this permission:

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

If a job service is declared in the manifest but not protected with this permission, that service will be ignored by the OS.

Constant Value: "android.permission.BIND_JOB_SERVICE"

Public constructors

JobService

added in API level 21
JobService ()

Public methods

jobFinished

added in API level 21
void jobFinished (JobParameters params, 
                boolean needsReschedule)

Call this to inform the JobManager you've finished executing. This can be called from any thread, as it will ultimately be run on your application's main thread. When the system receives this message it will release the wakelock being held.

You can specify post-execution behaviour to the scheduler here with needsReschedule . This will apply a back-off timer to your job based on the default, or what was set with setBackoffCriteria(long, int). The original requirements are always honoured even for a backed-off job. Note that a job running in idle mode will not be backed-off. Instead what will happen is the job will be re-added to the queue and re-executed within a future idle maintenance window.

Parameters
params JobParameters: Parameters specifying system-provided info about this job, this was given to your application in onStartJob(JobParameters).

needsReschedule boolean: True if this job should be rescheduled according to the back-off criteria specified at schedule-time. False otherwise.

onStartJob

added in API level 21
boolean onStartJob (JobParameters params)

Override this method with the callback logic for your job. Any such logic needs to be performed on a separate thread, as this function is executed on your application's main thread.

Parameters
params JobParameters: Parameters specifying info about this job, including the extras bundle you optionally provided at job-creation time.

Returns
boolean True if your service needs to process the work (on a separate thread). False if there's no more work to be done for this job.

onStopJob

added in API level 21
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(JobParameters, boolean).

This will happen if the requirements specified at schedule time are no longer met. For example you may have requested WiFi with setRequiredNetworkType(int), yet while your job was executing the user toggled WiFi. Another example is if you had specified setRequiresDeviceIdle(boolean), and the phone left its idle maintenance window. You are solely responsible for the behaviour of your application upon receipt of this message; your app will likely start to misbehave if you ignore it. One immediate repercussion is that the system will cease holding a wakelock for you.

Parameters
params JobParameters: Parameters specifying info about this job.

Returns
boolean True to indicate to the JobManager whether you'd like to reschedule this job based on the retry criteria provided at job creation-time. False to drop the job. Regardless of the value returned, your job must stop executing.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)