Handler vs AsyncTask

前端 未结 8 1606
猫巷女王i
猫巷女王i 2020-11-28 18:36

I\'m confused as to when one would choose AsyncTask over a Handler. Say I have some code I want to run every n seconds which will update the UI. Why would I choose one ove

8条回答
  •  时光取名叫无心
    2020-11-28 18:56

    The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread.

    AsyncTask provides a simple method to handle background threads in order to update the UI without blocking it by time consuming operations.

    The answer is that both can be used to update the UI from background threads, the difference would be in your execution scenario. You may consider using handler it you want to post delayed messages or send messages to the MessageQueue in a specific order.

    You may consider using AsyncTask if you want to exchange parameters (thus updating UI) between the app main thread and background thread in an easy convinient way.

提交回复
热议问题