处理器VS的AsyncTask处理器、VS、AsyncTask

2023-09-12 02:33:38 作者:任岁月静好

我很困惑,当一个人会选择的AsyncTask了处理程序。说我有一些code我想运行每n秒,这将更新UI。为什么我会选择一个比其他?

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 over the other?

推荐答案

IMO,AsyncTask的编写提供了方便,易于使用的方式来实现在Android应用程序的后台处理,而不用担心太多的低级详细信息(线程,消息循环等)。它提供了回调方法,帮助安排任务,并以在需要时方便地更新UI。

IMO, AsyncTask was written to provide a convenient, easy-to-use way to achieve background processing in Android apps, without worrying too much about the low-level details(threads, message loops etc). It provides callback methods that help to schedule tasks and also to easily update the UI whenever required.

然而,值得注意的是,使用的AsyncTask时,显影剂被提交到它的局限性,这导致由于设计决定的类的作者注意到是重要的。对于如最近,我发现有一个限制,可以使用AsyncTasks安排作业的数量。

However, it is important to note that when using AsyncTask, a developer is submitting to its limitations, which resulted because of the design decisions that the author of the class took. For e.g. I recently found out that there is a limit to the number of jobs that can be scheduled using AsyncTasks.

处理程序更加透明两个,可能给你更多的自由;所以如果你想要的东西更多的控制,你会选择处理程序,否则AsynTask会工作得很好。

Handler is more transparent of the two and probably gives you more freedom; so if you want more control on things you would choose Handler otherwise AsynTask will work just fine.