1

Most people using asynctask for long running operations instead of thread.

But the documentation says that:

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. 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 package such as Executor, ThreadPoolExecutor and FutureTask.

My question is if ui updation is not a concern, what is the problem I should face when I use asynctask for long running background operations?

jarlh
  • 42,561
  • 8
  • 45
  • 63

2 Answers2

2

AsyncTask is tied to the lifecycle of the activity, so it's very easy to mess its state in configuration changes / other events.

Personally, I recommend you to use HTTP libraries, like Ion or similar. It's easy to use, and gives you out-of-the-box progress control.

webo80
  • 3,365
  • 5
  • 35
  • 52
0

For downloading large amount of files or executing many asynchronous and time consuming operations in Android application, I would use the following elements:

Moreover, Android 5 has built-in so called JobScheduler, which does similar thing as Android Priority Job Queue, but if you want to supprot pre-Lollipop devices, you should choose first solution.

If you have single task and you don't need queue of tasks, you can use RxJava inside Service instead of Android Priority Job Queue.

Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39
  • 1
    NOTE: you can't use just Service for a long operations. You should start background thread inside it, otherwise Service will be invoked in the main/ui thread and will lock UI. http://developer.android.com/guide/components/services.html – krossovochkin May 18 '15 at 09:39
  • Yes, you are right. I've written that we should use Android Priority Job Queue or RxJava inside Service. These libraries are capable to execute time consuming operations in a separate thread. Service is long running operation and it doesn't depend on Activity lifecycle, but it's not executed in a separate thread. – Piotr Wittchen May 18 '15 at 09:42