6

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?

Oscar Méndez
  • 937
  • 2
  • 13
  • 39
  • Did you find any solution to this ? I want to achieve something similar. But not able to come up with feasible solution. – Dory Feb 04 '20 at 12:04

4 Answers4

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 :)

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.

Hassan Naqvi
  • 424
  • 6
  • 18
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.

Rahul
  • 19,744
  • 1
  • 25
  • 29
  • And what happens if you want to inform the user about the constraints and not to wait until they are meet? – Oscar Méndez Jun 07 '18 at 22:59
  • You have a `LiveData` using which you can observe the progression of `WorkStatus`. So you can show progress. – Rahul Jun 07 '18 at 23:40
  • 1
    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" – Oscar Méndez Jun 07 '18 at 23:45
  • 1
    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. – Rahul Jun 08 '18 at 01:11
  • 1
    @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... :/ – Alessandro Mautone Nov 13 '19 at 10:26
  • @AlessandroMautone Any feasible solution to this – Dory Feb 04 '20 at 12:05
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.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • 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 logic – Alessandro Mautone Nov 13 '19 at 11:20