Since Android 8 services, previously used for background tasks, were severely cut in rights and opportunities.

Now in Android we have WorkManager, wich helps use background tasks.

For start using WorkManager, we need to connect it:

 

Base classes:

  • Worker – task. Need to extend it and decribe work in  doWork()  method
  • WorkRequest – schedule. Must include class, wich extends Worker, but we can add details: conditions or timers. Have unique generated identifier, with which you can, for example, cancel a task. Contains helper classes:
    • Builder – construction helper class
    • Constraints – constraint builder
  • WorkManager – queues and manages processes.WorkRequest object will send in WorkManager, for queue task. WorkManager schedules the task in such a way as to balance the load on the system resources, while respecting the specified limitations.
  • WorkInfo – contains information about a specific task. WorkManager provides LiveData for each WorkRequest object. LiveData contains a WorkInfo object. Watching this data, you can determine the current status of the task and get any values returned after it is completed.

So there is class declaration:

Now we can run it:

Creating constraints:

Task canceling:

Adding tags (each task can have several):

Repeating task:

Here, in brief, that’s all. CU

Leave a Reply

Close Menu