Knowledge Drilling
Knowledge Drilling

Reputation: 1036

can JobService run immediately when it runs only once?

Can JobService run immediately when it runs only once?

i am currently testing JobService, i use following code

            JobScheduler jobScheduler = getSystemService(JobScheduler.class);
            jobScheduler.schedule(new JobInfo.Builder(1,
                    new ComponentName(getApplicationContext(), SyncJobLollipop.class))
                    .setMinimumLatency(1)
                    .setOverrideDeadline(1)
                    .build());

But it runs immediately, is this because i don't use periodic? Or it can run directly? I want to use this instead of Thread.

And can i run it again just after it is finished?

Upvotes: 1

Views: 1520

Answers (1)

Victor Rattis
Victor Rattis

Reputation: 544

Can JobService run immediately when it runs only once?

When you use JobScheduler API, it promises you that it will run your JobService when the criteria are done, but not necessarily immediately. Although if your application is in the foreground (the user is using it) it will run your JobService more rapidly. I saw cases where an app that was never used by the user had a big delay to run the job (this job would execute only once).

But it runs immediately, is this because I don't use periodically? Or it can run directly?

When a job is scheduled as periodic it has a default minimum time of 15 min to run (it does not schedule a job as periodic less than 15 min) and a common job doesn't have this limitation. But it doesn't mean that the job would be executed immediately after the conditions are met, as I explained before.

And can I run it again just after it is finished?

You can reschedule the JobService again when it is finished. I already implemented that and it works well. But if you schedule this JobService a lot, the system is going delay the execution of your job.

Periodic jobs doesn't need rescheduling, since after the predetermined amount of time the job is going to be executed again.

Follow my draft solution:

class MyJobService extends JobService {
    public boolean onStartJob(JobParameters params)
        /* Here is run my task */
        scheduleJob(); // 
        return false;
    }

    public static void scheduleJob() {
        /* code to schedule this job service. */
    }
}

Upvotes: 3

Related Questions