【问题标题】:Android Priority-Job-Queue keep retrying until successAndroid Priority-Job-Queue 不断重试直到成功
【发布时间】:2018-01-29 17:21:25
【问题描述】:

所以我必须从服务器下载一堆图像文件,我使用的是Priority-Job-Queue。到目前为止似乎工作正常,我正在使用简单的AsyncTask 进行下载部分。

因为我希望下载图像无论如何我只在shouldReRunOnThrowable() 回调中添加了RetryConstraint.RETRY。我还添加了android.permission.RECEIVE_BOOT_COMPLETEDManifest.xml 的权限

这是正确/最好的方法吗,所以如果出现任何问题并且由于错误而未下载某些图像,作业队列将尝试一次又一次地下载它们,直到作业成功完成?

谢谢!

@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {

    return RetryConstraint.RETRY;
}

【问题讨论】:

标签: android android-jobscheduler android-priority-jobqueue


【解决方案1】:

查看source codecancelForDeadlinegetRetryLimit() 约束,您应该满足这些约束才能继续重试您的工作。

对于第一个你只是不要 overrideDeadlineToCancelInMs for Params 对象,所以在这种情况下cancelForDeadline 是总是假的。

对于第二,您必须在工作中覆盖getRetryLimit 方法,例如:

protected int getRetryLimit() {
    return Integer.MAX_VALUE;
}

最后如果你达到重试限制,你可以重新安排你的工作:

@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
    if (cancelReason == CancelReason.REACHED_DEADLINE) {
        jobManager.addJobInBackground(new FetchImageJob());
    }
}

现在对我来说,它看起来会一直运行到成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2015-09-09
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多