What happens if I don't call jobFinished for the JobService?

Out of interest and better understanding of error scenarios: I have JobService

. As onStartJob

I return true

to announce JobManager

that there is more work. The Android documentation mentions that I need to call jobFinished

so that I JobManager

know the job is complete.

Now I am wondering (and there will probably be an error on it): What happens if I never call jobFinished

? Will Android be able to cancel the job at some point? Will it allow new instances of JobService to be created? Or will it prevent additional services from starting? Is there a maximum number of JobServices that can coexist?

+3


source to share


1 answer


What happens if I never call jobFinished?

The JobService keeps a wakelock for as long as it (or thinks it) does the job. In the event that you return true

from onStartJob()

, you are responsible for notifying the system when the job is complete.

By calling jobFinished()

, you indicate that the job is done and the JobService knows it can release the wakelock. If you don't call jobFinished()

, wakelock is not released and will continue to pump out the processor.

At some point, Android will kill the job service?

JobService is a subclass of a service, so yes. Under certain conditions, the OS kills services (for example, at least in memory).

Will it allow creating new instances of JobService? Or will it prevent additional services from starting? Is there a maximum number of JobServices that can coexist?

  • You are not manually creating / creating JobServices.
  • The JobService will only process one job at a time.


If you schedule()

create new jobs for the same JobService and with the same JobInfo, the current job will stop and a new one will start.

int schedule (JobInfo job)

      

Plan the task to be completed. Any current scheduled job with the same ID with new information in JobInfo will be overwritten. If a job with this ID is currently running, it will be stopped.


If you are enqueue()

new jobs for the same JobService, they will only run after the current job has finished. If you do not call jobFinished()

, the next job in the queue will not run.

int enqueue (JobInfo job, work)

      

Similar to Schedule (JobInfo), but allows you to start a job on a new or existing job. If a job with the same ID is already scheduled, it will be overwritten by the new JobInfo, but any previously started job will remain and will be submitted the next time it starts. If a job with the same identifier is already running, a new job will be installed for it.

Executing job queues for the same JobService works slightly differently than scheduling them with schedule()

. For complete reference see his docs .

+4


source







All Articles