Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why should i use android service instead of java thread

I am confused with android services and java thread.

Please help me to understand in which scenario i should use them.

As per my understanding

Service run in background ,so do thread.

Service is to be used for activities like network operation or playing mp3 in background,so do threads.

So whats actual difference between them and when to use each of them.

like image 738
kapil Avatar asked Mar 15 '12 20:03

kapil


People also ask

What is the difference between services and threads in Android?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.

Why do we need service in Android?

Services in Android are a special component that facilitates an application to run in the background in order to perform long-running operation tasks. The prime aim of a service is to ensure that the application remains active in the background so that the user can operate multiple applications at the same time.

Can thread be used instead of service in Android?

There is never a choice between using a Thread and using a Service, as they are distinct questions which must be answered independently. To add to my previous comment, while the Thread and Service concepts are normally orthogonal, the IntentService class represents a canned combination of the two ideas.

Is multithreading possible in Android?

Developers multithread Android applications in order to improve their performance and usability. By spinning off processor- or resource-intensive tasks into their own threads, the rest of the program can continue to operate while these processor intensive tasks finish working.


1 Answers

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.

like image 51
dzeikei Avatar answered Oct 13 '22 20:10

dzeikei