SaveImageToFileWorker doesn't execute even after the device has been plugged in
The 12th section of the codelab (the part on setting constraints) says that once the device or emulator is plugged in, SaveImageToFileWorker will stop being suspended by the 'requiresCharging' constraint but when the app is run on an emulator or device using Android 10 or Android 11, the function does not resume even when it is plugged and left to charge.
I just tried out the kotlin branch of this repo and SaveImageToFileWorker was working on my device. The task resumed and finished running after I re-plugged in the device. (Pixel 2 running Android 10)
Can you give the kotlin branch a try and see if you can still reproduce the issue? That branch is supposed to be the completed version of the codelab.
Alright, I’ll run that branch and see if it works.
Hey @dev-darrell ! Were you able to give this a try to see if the issue is reproducible?
Hi @calren! I'm so sorry I didn't get back to you earlier, I was preparing for my android cert exams. (I passed btw 😄)
I ran the kotlin branch and could still reproduce the issue where the image doesn't save if the device isn't charging but still doesn't when it's connected. What I found now is that the worker will only save the image if the battery is charging at 90% or above. The image won't get saved if the battery is at any percentage below that, even though it's charging. I also noticed that if you set the emulator to not have any charger connection and the battery status to "Unknown", the image will get saved immediately, as though the device is charging.
If you set your emulator's battery percentage to be around 60% and not charging, then after the other workers are done set it up as charging again, you should be able to reproduce the issue.
Dear Madam/Sir,
Good afternoon, how are you? I am having the same issue as well. Issue: The app is stuck in a loading state even if I plug in the charger. The issue is on the the Java Branch The code Lab says, "When the device is not charging, it should hang out in the loading state until you plug it in." I plug the phone in; the app is still displaying the loading animation. Goal: I would like to write a code that makes OneTimeWorkRequest recheck if the constraint has been satisfied; also, change the loading state animation. Question: Can you provide any resources and tips that will allow me to accomplish my goal?
5 min and a half video of the problem: https://drive.google.com/file/d/1cheaPFE64e-hnZiOIQ_L22EeSs_aHbN9/view?usp=sharing
Code Lab: https://developer.android.com/codelabs/android-workmanager-java#10
By the way @dev-darrell , congratulations
I was having the same problem but realized my emulator wasn't really updating when I changed the battery settings with extended controls.

The only way I got my emulator to register the changes was to restart the emulator. Then it picked up my changes. If anyone has a better way to have the emulator refresh and see adjustments in the Extended Controls, please share :) I hope this might help some of you.