Maurice
Maurice

Reputation: 2259

Job running at application startup and shouldn't

I have an android-job from the Evernote lib. The job is set to run periodically on certain circumstances. So the job build is only called when I want to start a job periodically (which is not at the beginning of my app)

Create a JobService:

class JobService : Job() {

    companion object {

        private var jobId = 0

        fun scheduleJobPeriodically(ms: Long) {
            if (jobRequested()) {
                cancelJob()
            }
            jobId = JobRequest.Builder("JOB_TAG")
                    .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
                    .setPeriodic(ms, TimeUnit.MINUTES.toMillis(5))
                    .setRequirementsEnforced(true)
                    .build()
                    .schedule()
        }

        fun cancelJob() {
            JobManager.instance().cancel(jobId)
        }

    }

    override fun onRunJob(params: Params): Result {
        Timber.d(">>>>>>>>>>>Job is Running")
        Result.SUCCESS
    }
}

A JobCreator:

class MyJobCreator : JobCreator {
  override fun create(tag: String): Job? {
    return when (tag) {
        "JOB_TAG" -> {
            JobService()
        }
        else -> null
    }
 }
}

In application class add the required line to register the job:

JobManager.create(this).addJobCreator(MyJobCreator())

At this point the application is started and the JobService class is created. (OK) But in this class, there is this method scheduleJobPeriodically, and this method is fired and it shouldn't, because I don't explicitly start it. I have started my app in debug mode and I am sure that the periodic creation is not called directly, as well as the JobRequestBuilder that set the tag for a job. But the code in the onRunJob is fired and it's the issue.

Thank in advance.

Upvotes: 3

Views: 159

Answers (0)

Related Questions