gpt4 book ai didi

android - JobScheduler 问题 android

转载 作者:行者123 更新时间:2023-11-29 16:51:38 27 4
gpt4 key购买 nike

jobscheduler 每 10 分钟定期运行一次。一旦应用程序运行并保持每 10 分钟定期运行一次,我必须做些什么才能运行计划。现在发生的情况是:安装应用程序后,运行计划需要 10 分钟。如何让它开始运行然后每 10 分钟重复一次?我有用于在 onStartJob() 中将数据更新到服务器的代码。但上传时间也从 7 分钟到 25 分钟不等。我想每 10 分钟上传一次数据,但它随机变化。这是为什么?

JobInfo jobInfo =
new JobInfo.Builder(MYJOBID, jobService).setPeriodic(600000).
setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).
setRequiresCharging(false).
setRequiresDeviceIdle(false).
setPersisted(true).
setExtras(bundle).
build();

int jobId = jobScheduler.schedule(jobInfo);
if(jobScheduler.schedule(jobInfo)>0){
Toast.makeText(LiveTrack.this,
"Successfully scheduled job: " + jobId,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LiveTrack.this,
"RESULT_FAILURE: " + jobId,
Toast.LENGTH_SHORT).show();
}

.

public class MyJobService extends JobService {

@Override
public boolean onStartJob(JobParameters jobParameters) {
new MyDownloadTask().execute();
return false;
}
}

最佳答案

I want to upload data every 10 mins but it varies randomly. Why is that?

JobScheduler API 不 promise 以精确的时间间隔重复。

But the upload time is from 7 min to 25 mins too.

根据setPeriodic() reference :

You have no control over when within this interval this job will be executed, only the guarantee that it will be executed at most once within this interval.

不过,我们应该最多间隔 20 分钟收到一次回调。让我们看看 JobInfo.Builder source code .从 setPeriodic(long) 开始:

 public Builder setPeriodic(long intervalMillis) {
return setPeriodic(intervalMillis, intervalMillis);
}

好吧,它称之为重载表亲。其中说:

Specify that this job should recur with the provided interval and flex. The job can execute at any time in a window of flex length at the end of the period.

哇,在我们的案例中,弹性长度也是 10 分钟?没那么快:

/**
* Specify that this job should recur with the provided interval and flex. The job can
* execute at any time in a window of flex length at the end of the period.
* @param intervalMillis Millisecond interval for which this job will repeat. A minimum
* value of {@link #getMinPeriodMillis()} is enforced.
* @param flexMillis Millisecond flex for this job. Flex is clamped to be at least
* {@link #getMinFlexMillis()} or 5 percent of the period, whichever is
* higher.
*/

A minimum value of getMinPeriodMillis() is enforced.

:|

你问的最短期限是多少?

MIN_PERIOD_MILLIS = 15 * 60 * 1000L;//15 分钟

所以您对 setPeriodic(60000) 的调用没有完成任何事情。最短时间保持在 15 分钟以内。

JobScheduler 并不是真的要用于精确的重复周期。事实上,它的构建是因为大多数应用程序都在滥用提供此(精确重复)功能的 AlarmManger api。

关于android - JobScheduler 问题 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46293368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com