tags:

views:

93

answers:

2

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?

+6  A: 

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.

Andrey
I was writing an answer, but this is what I was about to write. So +1.
Cristian
As an actual Android Developer I can only say totally correct. Go with the async task if you only download one file and with a thread or service if you are downloading and uploading constantly... good answer andrey
Janusz
Thanks everyone! I'll use the AsyncTask class.
Yoo Matsuo
AsyncTask is very useful to update UI after task finished. It don't block your UI (Activity). If you plan to use Thread in long-runing case, to create a service to hold the thread is better.
qrtt1
A: 

These two options have an equal probability of being killed while download is in progress (when user switches to another app). Still, AsyncTask is less mess. For downloading large files, consider using a Service.

alex