我正在尝试使用JobScheduler每3秒安排一次作业。
我已经准备好了Job.Builder,但是仍然在安排工作时,仍未调用onStartJob()

我不确定我缺少什么,请帮助我。JobBuilder:

ComponentName componentName = new ComponentName(this,JobSchedulerService.class);

JobInfo.Builder builder = new JobInfo.Builder(0,componentName)
                    .setPeriodic(5 * 1000);

JobScheduler jobScheduler = (JobScheduler) getBaseContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
int id = jobScheduler.schedule(builder.build());


Log.i("Service","ID is :"+id);

JobService内部,
public class JobSchedulerService extends JobService {

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Message message = new Message();
        message.what = Utils.JOB_SCHEDULER_WHAT;
        message.obj = jobParameters;
        handler.sendMessage(message);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }

    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            JobParameters jobParameters = (JobParameters)msg.obj;
            Log.i("Service:","ServiceID:"+jobParameters.getJobId());
        }
    };
}

请让我知道如何完成此操作。

最佳答案

Job Scheduler的最小setPeridic值为15分钟,因此,如果您耗时少于15分钟(如您的情况),jobScheduler只会将您的定期值限制为15分钟。考虑等待15分钟或使用setOverrideDeadline(long)。见https://catinean.com/2014/10/19/smart-background-tasks-with-jobscheduler/

关于android - JobScheduler setPeriodic永远不会被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48283996/

10-10 09:17