5

I need to run a background task through flutter workmanager but only when app is closed. How to achieve such a behavior. Code sample would be good. Thanks in advance

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
anass naoushi
  • 859
  • 2
  • 10
  • 25

2 Answers2

1

Please note that, WorkManager is used to achieve opportunistic and guaranteed execution of work.

So I would suggest in first place that, do not worry about "running task when the app is closed".

Because the WorkManager is designed in such a way that it will execute the task in a best possible performant way. i.e. without blocking your smooth app flow and without blocking Android system resources.

Still, if you need to run your work under specific condition then use Workamanger Constraints

SVK
  • 676
  • 1
  • 5
  • 17
0

The closest solution to your question would be to use the Constraint: DeviceIdle

From the docs:

When set to true, this requires the user’s device to be idle before the work will run. This can be useful for running batched operations that might otherwise have a negative performance impact on other apps running actively on the user’s device.

This way, the background task will run only when the device is idle and not being used actively.

AFAIK, there's no way to instruct WorkManager to run only when your app is closed.

rehman_00001
  • 1,299
  • 1
  • 15
  • 28