Android Priority-Job-Queue keep retrying until success

强颜欢笑 提交于 2019-12-08 11:39:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!