Job Scheduler not running within set interval

前端 未结 6 900
离开以前
离开以前 2020-12-09 19:59

I\'m trying to use the android Job Scheduler API and all I\'m trying to do is have the Job Scheduler run every 5 seconds. However when I run it, the corresponding service is

6条回答
  •  时光取名叫无心
    2020-12-09 20:29

    I was having this problem and after review some blogs and the official documentation, I realised that JobScheduler is having difference behavior on Android N(24 and 25). JobScheduler works with a minimum periodic of 15 mins.

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public static void setJobScheduler(Context context){
            Log.v(TAG, "Job Scheduler is starting");
            JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
            ComponentName serviceName = new ComponentName(context, JobService.class);
            JobInfo jobInfo;
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
                        .setPeriodic(900000)
                        .build();
            }else{
                jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
                        .setPeriodic(Constants.TIME_INTERVAL)
                        .build();
            }
            jobScheduler.schedule(jobInfo);
        }
    

提交回复
热议问题