安卓的AsyncTask的长期运行的操作 [英] Android AsyncTask for Long Running Operations

查看:166
本文介绍了安卓的AsyncTask的长期运行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里引用的文档AsyncTask的发现,它说:

Quoting the documentation for AsyncTask found here, it says

AsyncTasks理论上应该用于短操作(几秒钟之最。)如果你需要保持运行很长一段时间的线程,强烈建议您使用的java.util.concurrent中提供的各种API pacakge如执行器,的ThreadPoolExecutor和FutureTask提供。

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

现在我的问题是,为什么?该doInBackground函数运行关闭UI线程。因此,我们可以通过什么样的危害早已运行的操作吗?

Now my question arises why ? The doInBackground function runs off the UI thread. So what harm can we have by have long running operation here ?

推荐答案

这是一个很好的问题,这需要时间作为一个Android程序员要充分认识这个问题。事实上AsyncTask的有涉及两个主要问题:

It is a very good question, it takes time as an Android Programmer to fully understand the issue. Indeed AsyncTask have two main issues that are related :

  • 在他们不好绑在活动周期
  • 的内存泄漏很容易。

RoboSpice 动机应用程序(<一href="https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations">available在谷歌播放),我们详细回答这个问题。它会给一个深入了解AsyncTasks,装载机,他们的特点和弊端,也为您介绍的网络请求一个替代的解决方案:RoboSpice。 网络的要求是Android的一个共同的要求,本质上是长期运行的操作 。 下面是从应用程序的摘录:

Inside the RoboSpice Motivations app (available on Google Play) we answer that question in detail. It will give an in-depth view of AsyncTasks, Loaders, their features and drawbacks and also introduce you to an alternative solution for network requests : RoboSpice. Network requests are a common requirement in Android and are by nature long running operations . Here is an excerpt from the app :

AsyncTasks不按照活动实例的生命周期。如果你开始一个活动里面的AsyncTask的,你旋转设备,该活动将被销毁,新的实例将被创建。但AsyncTask的不会死。直到它完成它会继续活下去。

AsyncTasks don't follow Activity instances' life cycle. If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes.

它完成时,该AsyncTask的将不会更新新的活动的UI。事实上,它更新活动的前实例 已不再显示。这可能会导致类型java.lang.IllegalArgumentException异常的例外:查看不附着于窗口管理器如果 使用,例如,findViewById检索活动内的图。

And when it completes, the AsyncTask won't update the UI of the new Activity. Indeed it updates the former instance of the activity that is not displayed anymore. This can lead to an Exception of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity.

它是制造AsyncTasks作为内部类的活动非常方便。作为AsyncTask的需要操纵的观点 活动中,当任务完成或正在进行中,使用一个内部类的活动似乎方便:内部类可以 外部类的直接访问任何领域。

It is very convenient to create AsyncTasks as inner classes of your Activities. As the AsyncTask will need to manipulate the views of the Activity when the task is complete or in progress, using an inner class of the Activity seems convenient : inner classes can access directly any field of the outer class.

然而,这意味着内部类将举行其外部类的实例一种无形的参考:活动

从长远来看,这将产生一个内存泄漏:如果AsyncTask的持续时间不长,它保持活动活着 而Android的想摆脱它,因为它可以不再显示。该活动不能被垃圾收集,这是一个中央 机制为Android为preserve资源在设备上。

On the long run, this produces a memory leak : if the AsyncTask lasts for long, it keeps the activity "alive" whereas Android would like to get rid of it as it can no longer be displayed. The activity can't be garbage collected and that's a central mechanism for Android to preserve resources on the device.

这真的是一个非常非常糟糕的主意,使用AsyncTasks长时间运行的操作。然而,他们很适合短期居住的,如更新1〜2秒后视图。

It is really a very very bad idea to use AsyncTasks for long running operations. Nevertheless, they are fine for short living ones such as updating a View after 1 or 2 seconds.

我鼓励你下载<一个href="https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations">RoboSpice动机应用,它确实说明了这种深入的,并提供了不同的方式做一些后台操作的样本和示范。

I encourage you to download the RoboSpice Motivations app, it really explains this in-depth and provides samples and demonstrations of the different ways to do some background operations.

这篇关于安卓的AsyncTask的长期运行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆