Quoting the documentation for AsyncTask found here, it says
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.
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 ?
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 :
- they are poorly tied to the activity life cycle
- the create memory leaks very easily.
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 :
The AsyncTask and Activity life cycle
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.
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.
Memory leak issue
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.
Nevertheless, it means the inner class will hold an invisible reference on its outer class instance : the Activity.
quoted from: http://stackoverflow.com/questions/12797550/android-asynctask-for-long-running-operations
相关推荐
Android AsyncTask的简单Demo,对应Blog文章:http://blog.csdn.net/dolacmeng/article/details/50215519
Android AsyncTask用法和异步加载图片通过AsyncTask 从网络上异步加载一张图片
Android AsyncTask Demo 自己参考网络写的学习 demo
Android AsyncTask异步处理下载网页
AsyncTask 断点下载
代码为博客的示例代码,有问题请博客中留言:http://blog.csdn.net/lmj623565791/article/details/38614699
Android AsyncTask 文章集合
android AsyncTask中更新progressBar 。可用于http请求、下载文件等
Android Asynctask 先小试牛刀,了解一下各个方法执行过程,关注博客http://himici.com/
自己写了一个基于httpurlconnection和Asynctask的例子
android asyncTask的使用,通过简单的实例来说明Task异步类的使用
Android AsyncTask 同步下载图片,先是显示默认的图标,下载完成了立刻显示下载的图标,可以左右切换
AsyncTask的用法
Android AsyncTask Demo
在Android中使用AsyncTask和Handler线程间交互的方式,详情参见博客:http://www.cnblogs.com/plokmju/p/android_AsyncTask.html和http://www.cnblogs.com/plokmju/p/android_Handler.html
主要介绍了Android中异步类AsyncTask用法,分析总结了Async Task类的功能、特点及相关的使用技巧与注意事项,需要的朋友可以参考下
android AsyncTask的简单使用方法
简单的android AsyncTask 例子
AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主线程. 使用的优点: 简单,快捷 过程...
Android AsyncTask的缺陷和问题总结 在Android开发中,AsyncTask可以使得用户避免直接使用Thread类和Handler 来处理后台操作,适用于需要异步处理数据并将数据更新到界面上的情况。AsyncTask适用于后台操作只有几秒...