Update UI from a Workmanager

后端 未结 3 601
梦如初夏
梦如初夏 2021-01-22 12:41

How do I update the UI from Android WorkManager.

Android Workmanager can receive Boolean, Integer, Long, Float, Double, String

Documentation says:



        
相关标签:
3条回答
  • 2021-01-22 13:23

    First of all, WorkManager is an API for deferrable background work. If you are expecting to have the app in the foreground to display updates, it maybe the wrong API. WorkManager documentation covers this.

    Second, androidx.work.Data documentation states:

    This is a lightweight container, and should not be considered your data store. As such, there is an enforced MAX_DATA_BYTES limit on the serialized (byte array) size of the payloads. This class will throw IllegalStateExceptions if you try to serialize or deserialize past this limit.

    As WorkManager is part of Android Architecture Components a good solution is to use LiveData to pass status updates between the background job and the (eventually in foreground) UI.

    This is shown in the WorkManager codelab.

    Step 8. Tag and Display Work Status, covers this point.

    0 讨论(0)
  • 2021-01-22 13:32

    if you want to update ui from service you may use bound service, check this question for more details Android update activity UI from service

    0 讨论(0)
  • 2021-01-22 13:44

    For exactly that problem (I upload files with the WorkManager and want to monitor progress, not only result), I used EventBus. Super easy, put the listener anywhere you want - in my case the MainActivity, since I use a single activity pattern.

    0 讨论(0)
提交回复
热议问题