简体   繁体   English

Android N 中的定期作业(Nougat,7.0)

[英]Periodic Job in Android N (Nougat, 7.0)

Okay so I am not able to find any documentation or useful web pages about this.好的,所以我找不到关于此的任何文档或有用的网页。 Help me StackOverflow, you're my only hope.帮助我 StackOverflow,你是我唯一的希望。

Okay so originally my JobScheduler looks like this:好吧,原来我的 JobScheduler 看起来像这样:

JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
if(scheduler.getPendingJob(JOB_NUMBER) == null) {
    ComponentName componentName = new ComponentName(this, MyJobService.class);
    JobInfo info = new JobInfo.Builder(JOB_NUMBER, componentName)
            .setRequiresCharging(false)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setPersisted(true)
            .setPeriodic(60 * 60 * 1000L, 5 * 60 *1000)
            .build();
    int resultCode = scheduler.schedule(info);
    if (resultCode == JobScheduler.RESULT_SUCCESS) {
        Log.d(TAG, "Service is not running, Job " + String.valueOf(JOB_NUMBER) + " Scheduled.");
    } else {
        Log.d(TAG, "Service is not running, However job scheduling failed.");
    }
} else{
    Log.d(TAG, "Service is already scheduled.");
}

...Works perfectly in Oreo (v8.0). ...在奥利奥 (v8.0) 中完美运行。 However in Nougat, v7.0, The job gets 'scheduled' but never run.但是,在 Nougat v7.0 中,作业被“安排”但从未运行。 In another stackoverflow question I asked I found out that i can get it run by replacing setPeriodic() with the following:在我问的另一个 stackoverflow 问题中,我发现可以通过将setPeriodic()替换为以下内容来运行它:

.setMinimumLatency(1 * 1000)
.setOverrideDeadline(3 * 1000)

And with that, the service runs.这样,服务就会运行。 However, this isn't periodically, it will only run once.但是,这不是定期的,它只会运行一次。 I cannot find documentation / tutorials / examples that allow me to run a periodic job in Android Nougat.我找不到允许我在 Android Nougat 中运行定期作业的文档/教程/示例。 Can anyone help me with this?谁能帮我这个?

There are other stackoverflow questions on this exact same subject:关于这个完全相同的主题还有其他计算器溢出问题:

Job Scheduler not running on Android N 作业调度程序未在 Android N 上运行

Job Scheduler Not recurring in periodic in Android 7.0 (Nougat) 作业调度程序在 Android 7.0 (Nougat) 中不定期重复

However neither of them have definitive answers.然而,他们都没有明确的答案。

Last minute note: Well, it seems that passing the FlexMillis to setPeriodic() seemed to work.最后一刻注意:好吧,似乎将 FlexMillis 传递给setPeriodic()似乎有效。 I'm going to do more testing.我要做更多的测试。 I'm not sure what code I was running when the logcat fired, but I think by passing:我不确定当 logcat 触发时我正在运行什么代码,但我认为通过:

.setPeriodic(15 * 60 * 1000, 5 * 60 *1000)

Into setPeriodic it fired 10 minutes after the job was scheduled.进入setPeriodic它会在作业安排后 10 分钟触发。 However, unlike Oreo, the job isnt run when its first scheduled.但是,与奥利奥不同的是,作业在第一次调度时不会运行。 In Oreo as soon as I build the job, the job is run.在奥利奥中,一旦我构建了作业,作业就会运行。 Again, I can't find this mentioned anywhere in the documentation.同样,我在文档中的任何地方都找不到提到的这一点。

You should use https://developer.android.com/topic/libraries/architecture/workmanager/ .您应该使用https://developer.android.com/topic/libraries/architecture/workmanager/ This is new Android tool and it uses JobScheduler/AlarmManager and so on depending on situation.这是新的 Android 工具,它根据情况使用 JobScheduler/AlarmManager 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM