Skip to content Skip to sidebar Skip to footer

Android Start Running Jobscheduler At Specific Time

I want to start a JobScheduler at a specific time everyday, and finish it after 3 hours. I have the part of triggering the job every 20 min, and for 3 hours, but in JobInfo.Builde

Solution 1:

I achieved it in the following way (without AlarmManager), created a new Job (with a unique JOB_ID obviously), and told the JobScheduler to schedule it for sometime in the future using setOverrideDeadline().

Here's a snippet which might be helpful:

private JobInfo getJobInfoForFutureTask(Context context,
                                        long timeTillFutureJob){
        ComponentName serviceComponent = newComponentName(context, SchedulerService.class);

        returnnew JobInfo.Builder(FUTURE_JOB_ID, serviceComponent)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE)
                .setOverrideDeadline(timeTillFutureJob)
                .setRequiresDeviceIdle(false)
                .setRequiresCharging(false)
                .setPersisted(true)
                .build();
    }

Make sure to calculate the correct time offset at which you want to schedule the task and also remove any existing Job IDs from the JobScheduler.

Solution 2:

adding setMinimumLatency on jobInfo with the deference of the current time and the target time solves this issue.

JobInfojobInfo=newJobInfo.Builder(1, componentName)
                .setPersisted(true)
                .setBackoffCriteria(6000, JobInfo.BACKOFF_POLICY_LINEAR)
                .setMinimumLatency(1000 * 60)
                .build();

for the example above the scheduler will work after 60 secs.

Post a Comment for "Android Start Running Jobscheduler At Specific Time"