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?
I’m confused between thread and service on android. If I have to download some
Share
Doing Stuff in a Service does not make it run in the background. You have to create a Thread in your service as well to wait for the download to complete.
If you are using a thread that is local to an activity you will have problems with leaving the activity, sending it to the background or an interruption because of an incoming call etc. This may cause your app to be killed by the OS to free up memory. In this case your thread is lost and may not get restarted and make your app crash.
If you create a service you are not as likely to be killed then just a thread and you can specify the OS to restart the service after it gets killed by the OS. This makes the service a more secure choice for very long downloads. I use a service in one of my apps to download a 20 mb file and in this service create a thread that does the downloading.
A service only helps you to encapsulate the download and decouples it from the activity and gives you another state in the OS that will prevent the thread from just disappear because of low memory.