Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether a bound service or customized threads when download something?

I'm working on an Android project, in which I need to load some images to a GridView. The images could be in the cache, if cache missing, then query the Internet server. My design is to use two threads to do the loading task. One thread for reading cache and one thread for downloading. So there are 3 threads including the UI one. Each of them has its own message queue and uses Handler to communicate.

My question is whether I should use bound service in this situation instead? Actually I have realized the design above and it seems nothing wrong but GC is working very hard, which can be inferred from the logcat.

Another issue is that several threads exist when I use DDMS to monitor the threads. This is because the same loading mechanism is used in several Activities. I have let the threads quit its message loop while onPause() is called, I'm sure only two of them are alive in the same time. But I can see all of them in DDMS. (BTW, why the threads still exist? I have let thread = null;)

So in a word, my question is: Could this loading task benefit from a bound service?

like image 282
Alley003 Avatar asked Dec 02 '12 22:12

Alley003


People also ask

What is difference between started and bound services in Android?

Started services run until they are stopped or destroyed and do not inherently provide a mechanism for interaction or data exchange with other components. Bound services, on the other hand, provide a communication interface to other client components and generally run until the last client unbinds from the service.

What is a bound service?

A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).

What is bounded and unbounded services in Android?

A service is termed as bounded when an application component binds itself with a service by calling bindService() method. To stop the execution of this service, all the components must unbind themselves from the service by using unbindService() method.

When binding to a service What does the client do with the IBinder object?

After the client receives the IBinder, it can begin interacting with the service through that interface. onBind(): The system invokes this method by calling bindService() when another component wants to bind with the service (such as to perform RPC).


1 Answers

So why are you using multiple threads? You need to load an image in the background, and when done, display it in the UI. It really doesn't matter to the UI where the images comes from. And using multiple threads doesn't make it faster, just consumes more memory. Just use a single background thread: first it hits the cache, then downloads if no hit.

Services are for when you want do something that doesn't need a UI. If you need to update the UI in real time, a service doesn't make much sense.

like image 111
Nikolay Elenkov Avatar answered Oct 29 '22 08:10

Nikolay Elenkov