Android - How to check if Worker meet Constraints when is enqueued?
Asked Answered
W

4

7

How can I check if the Worker meets Constraints when is enqueued?

For example, if I need to download data from the internet and establish that the Worker only runs if there is an internet connection. How can I check at that moment if the Worker meet the Constraints to alert the user?

Or if I'm going to perform a task that can consume a lot of battery power and I want to show a Dialog saying "Start charging your Smartphone's battery to start"

Is there a way to do it from the WorkManager or do I have to do it from an external method?

Wren answered 7/6, 2018 at 21:52 Comment(1)
Did you find any solution to this ? I want to achieve something similar. But not able to come up with feasible solution.Bromidic
E
2

I think there should be some sort of callback provided by the library for when constraint(s) are not met (currently there is nothing like that).

I created a google issue here: https://issuetracker.google.com/issues/144367861

Feel free to star it so it can get more visibility :)

Emanuele answered 13/11, 2019 at 11:15 Comment(0)
S
1

USE getWorkInfoByIdLiveData().observe()

WorkManager.getInstance().enqueue(WorkRequest);

WorkManager.getInstance().getWorkInfoByIdLiveData(WorkRequest.getId())
    .observe(this, new Observer<WorkInfo>() {
        @Override
            public void onChanged(WorkInfo workInfo) {

                switch (workInfo.getState()) {
                    case ENQUEUED:
                        // TODO: Show alert here
                        break;
                    case RUNNING:
                        // TODO: Remove alert, if running 
                        break;
                    case SUCCEEDED:
                        // TODO: After work completed
                        break;
                    case FAILED:
                        break;
                    case BLOCKED:
                        break;
                    case CANCELLED:
                        break;
                }
            }
        });

WorkInfo.State has 6 states, ENQUEUED can be useful for you.

Seeley answered 6/11, 2020 at 14:18 Comment(0)
D
0

You don't have to. You can just set the constraints on the OneTimeWorkRequest and WorkManager will schedule the Worker when the constraints are met.

Dettmer answered 7/6, 2018 at 22:29 Comment(6)
And what happens if you want to inform the user about the constraints and not to wait until they are meet?Benedick
You have a LiveData using which you can observe the progression of WorkStatus. So you can show progress.Dettmer
I am observing the progress... but the observe only let me know the States: blocked, cancelled, enqueued, failed , running or succeeded. Take the example of network conection... the worker is enqueued util connected network is detected... but how can I inform the user "Hey you don´t have network conection... the wokr will never be complete"Benedick
There could be many reasons why your Worker is not running. Also when WorkManager is using JobScheduler, JobScheduler only tells WorkManager to do work when constraints are met.Dettmer
@OscarMéndez did you find a solution in the end? I got your point and I am looking to get something similar working. A solution could be to remove teh constraint and handle the exeception manually, delivering a retry result. But then you would loose the benefits of having a constraint, like it being triggered automatically when conditions are met... :/Emanuele
@AlessandroMautone Any feasible solution to thisBromidic
B
0

It would be as simple as removing the constraint which states that network connection is required to run the job.

You can simply schedule a periodic request (you can follow my answer on this SO for implementing periodic request) and when it is triggered, check if internet connection is enabled. If so then execute a task, otherwise simply post Notification or any other suitable way to notify user.

Bantling answered 23/6, 2018 at 7:3 Comment(1)
True, that is a possible solution, but I think the library should provide a way to know when constraints are not met. Then you could still use the constraint and have all the benefits it already provides without implementing extra logicEmanuele

© 2022 - 2024 — McMap. All rights reserved.