why should i use android service instead of java thread why should i use android service instead of java thread multithreading multithreading

why should i use android service instead of java thread


Let me give an analogy.

Activities and Service are like projects.

Activities are like external projects. This is what the clients(users) see.

Services are like internal projects. There might be several internal projects for 1 external project or none at all.

You can "pause" external project but the internal project that supports it can still continue.

Main Thread is like the boss in a company

The boss should never be held up by too much work since he shouldn't be late to meetings (UI freezing) or the client(user) will be unhappy.

Threads are like employees in a company.

The more you have, the more things you can do at the same time provided you have enough equipment(CPU speed) for all of them.

Multiple employees can work on the same project at the same time but the boss should really work only on the Activities.


Always: A service of your application is usable not only by other components of your application, but by other applications, too.

A service is for use in not-GUI parts of program.

Mostly: A service is something more independent, than a thread. Service is something more long-living than a thread. Service is something more complex than a thread.

BTW, threads do not run in background only. What runs in foreground, is a thread, too.


I believe the main difference is about Android system attitude. Service is a part of android infrastructure, so android recognizes service as a working part of application and considers killing service as a last option. Moreover, if your service is killed (e.g. because of memory lack) you can say system to restart it automatically, whenever resources available again. Moreover, you can tune up service priority in order to do it as important as foreground activity. As for threads, android does not recognize a thread as important part which must be kept. So usual threads has much more chances to be killed eventually.

For instance If you have an activity which start a working thread and then go background, as android do not recognize thread as a working part, it may think that application do nothing, because no activity or service running and kill the whole app, including the working thread.

Thus when you start a Service, you are telling system something like: "Hi. I'm doing some business here, don't kill me until I finish, please." and Android pay attention to your request.