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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With