I'm confused between thread and service on android. If I have to download some file from the server. It may be multiple files at a time. What should I choose in this situation, thread or services?
What i should choose in this situation thread or services?
It's not an "or". It's an "and". You use a background thread and a service.
A service is "in the background" from a UI standpoint, in that it has no UI. A service is not automatically "in the background" from a threading standpoint, in that onCreate()
, onDestroy()
, onStart()
, and onBind()
are all called on the main application thread, the same thread shared by all the activities of this application. Anything long-running, like a download, needs to be done outside of the main application thread, such as using an AsyncTask
.
If your downloads need to go on even if the activity that triggered them is destroyed, you need to use a service, with the service using an AsyncTask
or background thread to complete the download. Even better is to use an IntentService
, which combines a regular service with a background thread.
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