JobScheduler 问题 android
JobScheduler issues android
作业调度程序 运行 每 10 分钟定期执行一次。我必须在应用程序 运行 后立即对 运行 时间表做些什么,并每隔 10 分钟定期保持 运行ning。现在发生的情况是:安装应用程序后,运行 需要 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 不承诺以准确的间隔重复。
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.
哇,在我们的案例中,flex 长度也是 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 minutes
所以您对 setPeriodic(60000)
的调用没有完成任何事情。最短时间保持在 15 分钟。
JobScheduler
并不是真的要用于精确的重复周期。事实上,它的构建是因为大多数应用程序都在滥用 AlarmManger
api 提供此(精确重复)功能。
作业调度程序 运行 每 10 分钟定期执行一次。我必须在应用程序 运行 后立即对 运行 时间表做些什么,并每隔 10 分钟定期保持 运行ning。现在发生的情况是:安装应用程序后,运行 需要 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 不承诺以准确的间隔重复。
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.
哇,在我们的案例中,flex 长度也是 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 minutes
所以您对 setPeriodic(60000)
的调用没有完成任何事情。最短时间保持在 15 分钟。
JobScheduler
并不是真的要用于精确的重复周期。事实上,它的构建是因为大多数应用程序都在滥用 AlarmManger
api 提供此(精确重复)功能。