具有 "OR" 条件的 WorkManager NetworkType

WorkManager NetworkType with "OR" condition

我刚刚完成了一个关于 WorkManager 的代码实验室,我想知道如何使用它获得 "OR" 条件。检查 setRequiredNetworkType 后,我发现它只接受一个 NetworkType 类型的参数。

/**
 * Sets whether device should have a particular {@link NetworkType} for the
 * {@link WorkRequest} to run.  The default value is {@link NetworkType#NOT_REQUIRED}.
 *
 * @param networkType The type of network required for the work to run
 * @return The current {@link Builder}
 */
public @NonNull Builder setRequiredNetworkType(@NonNull NetworkType networkType) {
    this.mRequiredNetworkType = networkType;
    return this;
}

例如,我希望我的任务在 NetworkType.UNMETEREDNetworkType.CONNECTED 时运行。可能吗?


编辑:也许这不是一个很好的例子,我想知道是否有可能有一个 OR 条件用于 Constraint.

connected - Any working network connection is required for this work.

metered - A metered network connection is required for this work.

not_roaming - A non-roaming network connection is required for this work.

unmetered - An unmetered network connection is required for this work.

根据上述情况和您的用例,您可以使用 UNMETERED,这将同时指示 - 已连接和未计量

CommonsWare 在评论中回答了我的问题:

"I wanted to know if it's possible have an OR condition to use for Constraint." -- not in a single piece of work. You might be able to rig up multiple pieces of work with separate constraints, and try some coordination such that only one will be used