advanced patterns with WorkManager's OneTimeWorkRequest and Constraints on Android 14
I'm optimizing some code but I'm experiencing an scenario with WorkManager when trying to schedule a `OneTimeWorkRequest` that includes constraints. I have a use case where I want to ensure that the work only runs when the device is connected to Wi-Fi and the battery is not low. However, it seems that the constraints are being ignored, and the work executes regardless of the battery status. Here's the code I'm using: ```kotlin val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) .setRequiresBatteryNotLow(true) .build() val workRequest = OneTimeWorkRequestBuilder<MyWorker>() .setConstraints(constraints) .build() WorkManager.getInstance(context).enqueue(workRequest) ``` I thought that using `setRequiresBatteryNotLow(true)` would prevent the work from executing when the battery is low, but that's not happening. I’ve checked the battery settings on my device and it seems to be in a low state when the work runs, but it still triggers. I've also confirmed that I am using the latest WorkManager version (2.7.1) compatible with Android 14. I've tried adding logging in the `doWork()` method of `MyWorker` to see if it gives any hint as to why it's not respecting the battery constraint: ```kotlin class MyWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { override fun doWork(): Result { Log.d("MyWorker", "Work is starting...") return Result.success() } } ``` In the logs, I see the message indicating that the work has started, but no additional information about why the constraints failed. Has anyone else faced this scenario or have insights into how constraints work with WorkManager? Any advice on how to debug this further would be appreciated! This issue appeared after updating to Kotlin 3.11. Am I approaching this the right way? Could someone point me to the right documentation?