7

all I need to know is that when I can do some operation using an independent thread, why do I need Service. What is that, a Service can do but a Thread can't? I did go to through many of the posts but couldn't find a satisfactory answer.

SAMD
  • 421
  • 1
  • 3
  • 7

3 Answers3

49

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.

Though conceptually both looks similar there are some crucial differentiation.

1.Service - if it is destroyed while performing its job, in the middle by Android due to low memory scenario. Then android will make sure that it will restart your service, if you have returned START_STICKY or START_REDELIVER_INTENT from onStartCommand().

2.Thread - if it is destroyed by android in middle due to low memory, then android will not guarantee to restart it again. That means user lost his half work.

3.Service - is a component of android, so it has priority levels to be considered while destroying an application due to low memory.

4. Thread - is not a component of android, so android will not take thread priority into consideration while killing an application due to low memory.

I will try to explain this 3rd point.

Lets, say you have a requirement of connecting to internet from your activity. You can do it by using a service(with thread) or directly by creating a thread in activity. Consider the second scenario where you are connecting to internet in a thread. Then

i. What will happen if user closes the activity, while still thread is running in the background. Will that thread continue to run in back ground ? Answer is you can't really predict.

ii. Assume that in continuation for above scenario, even after killing activity your thread continued to do its intended operation. Then there is a low memory situation arises in your phone. Then this application will be the first susceptible app to be killed as there is no priority for this application.

So bottom line is: If you want to do some heavy background functionality then it is always better to have a service with thread. If you feel that that background functionality to be alive as long as your activity is alive, then go for activity with thread or activity with async task.

Hope it helps.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
user1923551
  • 4,684
  • 35
  • 29
13

The main benefit of a Service is access to a Context object which has an independent life cycle. This allows for reuse of common code by many activities and, in the case of public or exposed services, many applications.

A thread is a mechanism for doing work without blocking other work.

A service does not imply a thread and a thread does not imply a service. They are two very different things. I tend to use Services in situations where I need a context to preform work but want the code to be isolated from my activities to improve reusability.

Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • Ok.. If in case I pass the context in a worker thread then will it serve the purpose ? – SAMD Feb 06 '12 at 06:22
  • you haven't explained the case or the purpose so its hard for me to tell you. – Nick Campion Feb 06 '12 at 15:46
  • Actually I'm looking for some exceptional case where a Service is a necessity to be used. I was asked this question and couldn't answer, so I'm trying to seek help using Stack Overflow. – SAMD Feb 07 '12 at 06:55
  • 1
    A service is convenient for many things. Some examples of services I've made are DownloadService and a RadioPlaybackService. The main value of a Service is a separate life cycle which is not attached to the Activity being displayed. You will come up against a wall pretty quickly using just Threads as operations like orientation changes will cause threads associated with the activity to be destroyed. You can circumvent this using Executors hosted in the Application object or AsyncTasks. The value of a service is the life cycle and being able to have multiple activities access the same operation – Nick Campion Feb 07 '12 at 14:47
3

Difference between Service and Threaads

Please visit this link, I hope you will find answer in this link.

In short, the main difference between Service and Thread is that, Service runs on Main(UI) thread and Thread runes on its own thread. If we are using Service for long tasks, then it may cause block Main UI Thread.

Please visit given link for more detail.

mark
  • 503
  • 5
  • 27