Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread or services

Tags:

android

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?

like image 405
Thanh Le Avatar asked Aug 19 '10 10:08

Thanh Le


1 Answers

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.

like image 197
CommonsWare Avatar answered Oct 24 '22 20:10

CommonsWare