Android start running JobScheduler at specific time

后端 未结 2 797
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 08:08

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 fo

2条回答
  •  有刺的猬
    2020-12-29 08:54

    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 = new ComponentName(context, SchedulerService.class);
    
            return new 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.

提交回复
热议问题