Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async task versus service to download data

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!

like image 303
Carlos Porta Avatar asked Feb 09 '15 01:02

Carlos Porta


People also ask

What is difference between service and AsyncTask?

service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

Is Async task a service?

Services are mostly there to "exist". They are like an off-screen Activity , providing a reason for the app to stay alive, while other components take care of doing the "work". AsyncTasks do "work", but they will not, in and of themselves, keep a process alive.

What is the difference between async task and thread?

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.

Why async task is 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.


1 Answers

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.

like image 81
StenSoft Avatar answered Sep 21 '22 02:09

StenSoft