Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To download a large file, which is a better approach to use either AsyncTask or Thread?

Tags:

android

I've found a sample to download a large data file at the following link, http://code.google.com/p/apps-for-android/source/browse/#svn/trunk/Samples/Downloader

It seems to be pretty nice (I haven't yet tested it). But I also have read some posts at the stackoverflow to do the same thing by using the AsyncTask class, not using the Thread class as the above sample.

What I want to know is, which should I use to achieve downloading a file? And if AsyncTask is better, would you point me to a sample code?

like image 245
Yoo Matsuo Avatar asked Jun 16 '10 13:06

Yoo Matsuo


People also ask

What is difference between thread and AsyncTask?

Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread. Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once. Long task in general.

What is the use of AsyncTask class?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Why is AsyncTask deprecated?

This class was deprecated in API level 30.AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.


2 Answers

Disclaimer: i am not Android developer, answer comes from general experience.

Thread class is most suitable for long-running activities, not for asynchronous tasks. Except if you manage pool of workers, but still lifetime of thread is same or nearly same as application. Consider that creation of thread is expensive operation.

AsyncTasks and other helpers are usually for some single activities that you want to do in background so not to block the app. They are usually well managed by the platform and are cheap.

My opinion: use AsyncTask if you want to load pages occasionally. If your app will load pages all the time in the background consider thread.

like image 105
Andrey Avatar answered Nov 16 '22 03:11

Andrey


For understanding what has to be used one must understand the nature of the task we are about to perform. Suppose we are going to download large file.... would you being a user want to see it or rather let it run in the background?? i guess i dont mind running that task in the background(unless it is game and some graphics are being downloaded).

Taking this thought in mind, if we use the Asyntask, the user must have to keep the App open until the download operation has been completed; as three out of four methods of AsyncTask runs on the UI thread. (check out the link : https://developer.android.com/reference/android/os/AsyncTask) In the other case where we are using the AsyncTask to download graphics file for a game, it would be completely fine to go for it.

So I believe it is better to go for Thread or even better to go for Service to download the content so that one may continue to work further on the app/ close the app or even run some other app.

like image 36
Ashwinee Kumar Singh Avatar answered Nov 16 '22 03:11

Ashwinee Kumar Singh