问题
So I have to download a bunch of image files from a server and I'm using Priority-Job-Queue. So far seems to works fine and I'm using a simple AsyncTask
for the downloading part.
Since I want the images to be downloaded no matter what I only added RetryConstraint.RETRY
on shouldReRunOnThrowable()
callback. I have also added android.permission.RECEIVE_BOOT_COMPLETED
permission on Manifest.xml
Is this the right/best way so if there's any kind of problem and some images aren't downloaded due to an error, job-queue will try to download them again and again until the job is finished with success ?
Thanks!
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
return RetryConstraint.RETRY;
}
回答1:
Looking at the source code there are cancelForDeadline
and getRetryLimit()
constraints, that you should satisfy in order to keep retrying your job.
For the first one you just don't overrideDeadlineToCancelInMs
for Params
object, so in that case cancelForDeadline
is always false.
For the second one, you have to override getRetryLimit
method in your job, like:
protected int getRetryLimit() {
return Integer.MAX_VALUE;
}
And finally in case if you reached retry limit, you can schedule your job again:
@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
if (cancelReason == CancelReason.REACHED_DEADLINE) {
jobManager.addJobInBackground(new FetchImageJob());
}
}
Now for me it looks like it will run until success.
来源:https://stackoverflow.com/questions/48506689/android-priority-job-queue-keep-retrying-until-success