6

I want to know which is better to download files, async task or service?

My app has eight buttons, which one starts one direfferent download (which download has ~10MB). When the user clicks on one button or more to download data is better to use async task or service?

Thanks!

Carlos Porta
  • 1,224
  • 5
  • 19
  • 31

3 Answers3

6

In any case you should use AsyncTask because even service runs in the main (GUI) thread where no networking should be done. Whether to run the AsyncTask in a service or an activity depends on whether you want that download to continue in background.

StenSoft
  • 9,369
  • 25
  • 30
  • Thanks @StenSoft, so if the apps goes to background or the user change to another activity, async task stops? – Carlos Porta Feb 09 '15 at 01:55
  • 1
    No but without a service, the whole application can be killed by the system any time in background. – StenSoft Feb 09 '15 at 01:57
  • hmm know I understand what I have to use on which moment! So I'll use AsyncTask. You solved my boubt, thanks StenSoft!! – Carlos Porta Feb 09 '15 at 02:05
  • 8
    `Service` runs in the UI thread but `IntentService` uses it's own thread. `IntentService` allows your download to continue even if the user has closed app, and `AsyncTask` are ideally used for short operation [according to offical documentation](http://developer.android.com/reference/android/os/AsyncTask.html). Download operation can be long, for me it seems that using `IntentService` is a better solution. – Lawrence Choy Feb 09 '15 at 02:20
2

all above answers have good points. but the life-cycle issue is the most important thing you should consider. for instance, let's say you use asyncTask. so the user starts downloading and suddenly he/she rotates the screen and because you tied the asyncTask life-cycle to activity another asyncTask operation will be kicked off and result in a compulsive 10mb download. so considering this you should use service and asyncTask together to maintain life-cycle issue and UI thread networking issue.

update: Intent-service is a better solution because it receives requests in its own thread and goes offline when it doesn't have anything to do

hadi
  • 1,104
  • 8
  • 23
1

AsyncTask -- AsyncTask manipulate threads and/or handlers, if you can do that better with Looper and stuff why bother? AsyncTask is designed to be a helper class around Thread and Handler, and it should ideally be used for short operations (a few seconds at the most.).. how can you tell in production mode whether is not gonna take long? probably bad network, slow network,jammed network, phone restarting - and all these will probably make your downloading either corrupt or unfinished.. i am a user of apps, and i get pissed when i waste bundle on nothing..

if you ask me, use

Service --Serviceis made to run irrespective of what app/screen is visible and make if communicate with the UI if only it is available if not continue with download and save it, AsyncTask does not constitute a generic threading framework. always use threads, its cool, we all love it.

Elltz
  • 10,730
  • 4
  • 31
  • 59