Android 工作开始后工作管理器约束发生变化

Android Work manager constraint change after work is started

工作管理器是否会在工作处理过程中不满足约束条件后停止工作。 比如work需要charger connected constraint,接上充电器后开始工作,然后充电器断开,Work manager会不会因为不再满足约束而停止工作

如果在 worker 执行期间不再满足约束,WorkManager 将发出停止信号。
请记住,停工在 WorkManager 中是合作的,因此您的 Worker 将需要在执行 long 运行 loops/computation.
时实施 onStopped() callback and it can use the isStopped() 方法 来自 doc:

You should cooperatively abort any work you had in progress and release any resources your Worker is holding onto. For example, you should close open handles to databases and files at this point. There are two mechanisms at your disposal to understand when your Worker is stopping.

也来自文档:

Note: WorkManager ignores the Result set by a Worker that has received the onStop signal, because the Worker is already considered stopped.

如果您使用的是 CoroutineWorkers,它们将在定义的范围内自动处理停工。 来自 WorkManager's threading guide:

CoroutineWorkers handle stoppages automatically by cancelling the coroutine and propagating the cancellation signals. You don't need to do anything special to handle work stoppages.