Asynchronous task or service for loading data

I want to know what is the best way to upload files, an asynchronous task or service?

My application has eight buttons that start one download (which loads ~ 10 MB). When a user clicks one button or more to load data, is it better to use an asynchronous task or service?

Thanks!

+5
source share
3 answers

In any case, you should use AsyncTask , because even the service runs in the main (GUI) thread, where there should be no network. Whether or not to AsyncTask in a service or action depends on whether you want the download to continue in the background.

+5
source

AsyncTask - AsyncTask manipulate threads and / or handlers if you can do it better with Looper and stuff, why bother? AsyncTask is designed as a helper class around Thread and Handler, and should ideally be used for short operations (maximum seconds). .. , as you can indicate in production mode, is this going to take a lot of time? probably a bad network, a slow network, a stuck network, rebooting the phone - and all this will probably make your download either damaged or incomplete. I am an application user, and I get angry when I lose the bundle no matter what.

If you ask me, use

Service - Service starts regardless of what application / screen is visible and does, if it interacts with the user interface, if only it is available, if you do not continue loading and save it, AsyncTask not a common flow infrastructure. always use threads, that's cool, we all love it.

+1
source

all of the above answers have good points. but the life cycle issue is the most important thing you should consider. for example, let's say you are using asyncTask . so the user starts loading, and suddenly he rotates the screen, and since you bound the asyncTask life cycle to activity , another asyncTask operation will be launched and will result in a compulsive download of 10 MB. therefore, when considering this, you should use service and asyncTask together to support the life cycle problem and the problem with the network of user interface threads.

update : Intent service is the best solution because it receives requests in its thread and disconnects when it has nothing to do

+1
source

Source: https://habr.com/ru/post/1212925/


All Articles