IntentService:如何在工作队列中添加和删除意图?

IntentService: how to add and remove intents from the worker queue?

在 Android 他们让 IntentService 看起来像是在后台上传 pdf 列表时的方式。

如何实际访问工作队列以从工作队列中删除特定项目?如果由于某种原因上传项目失败,我也想重新将项目添加到队列中。

有什么想法吗?

您无法从队列中删除某些内容,但您可以使用以下内容将其标记为可跳过:

private static Collection<Object> cancelledThingIds;

public static void cancelThing(Object thingId){
    cancelledThingIds.add(thingId);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        final Object thing = intent.getExtra(EXTRA_THING);

        if(cancelledThingIds.contains(thing.getId()))
            cancelledThingIds.remove(thing);
        else{
            processThing(thing);
        }
    }
}

虽然重试项目要简单得多 - 只需为您的 intentservice 创建一个新的新 intent 并重新启动它。您可以在意图中包含诸如 "attempt number" extra 之类的内容,这样如果您尝试了太多次,就可以做其他事情。