How come Constraints.Builder().setRequiresDeviceIdle(boolean requiresDeviceIdle) requires API 23?

497 views Asked by At

Using the Constraints class to set constraints on a WorkRequest, it gives me a warning I need to have API 23 as my minimum to use the Constraints.Builder().setRequiresDeviceIdle(boolean requiresDeviceIdle) function.

Why is this? I thought the whole WorkManager API was suppose to work all the way back to API 14.

What are the alternatives?

1

There are 1 answers

1
Perraco On BEST ANSWER

If you check the actual setRequiresDeviceIdle source code (Ctrl + Click over it), you will see it is marked with a requirement of minimum API 23.

enter image description here

When it comes to any androidx library backport to lower API levels, some functionality may sometimes not be possible, because there are no equivalents for some functionality.

You will need to implement a custom-made workaround depending on your requirements. A solution could be to let your task start, and use a helper method to decide if it needs to be re-scheduled, or execute the task’s target logic.

For example, if you need the task to run only if the device is not idle (or the opposite), then you could create a helper method where you could use PowerManager.isInteractive (which goes down to API 20), or KeyguardManager.isKeyguardLocked (which goes down to 16), and then perform a best possible “idle state” decision. But it will not be a 100% bulletproof solution for APIs below 23.

As a side note, consider also the life utility of requiring such functionality, as at the time of this answer devices with API levels below 23 account only for 15% of the share market, which is quickly getting lower by the year, as anything below 23 is already very old and limited.