kapil
kapil

Reputation: 257

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.

Upvotes: 19

Views: 8645

Answers (7)

Arivu
Arivu

Reputation: 1

Why we need service is to avoid resource crunch. For example you opening an application after opening an another application so at the time your app added to the background task. While opening multiple application, ur app may killed by android system. So if ur app has service it won't be killed by the system because service is higher priority , even it may killed the app has service so that we using constant return type in onStartCommand(). Method. That's START_STICKY,START_NOT_STICKY and DELIVER_INTENT.

Upvotes: 0

mc.dev
mc.dev

Reputation: 2735

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.

Upvotes: 6

Rikin Prajapati
Rikin Prajapati

Reputation: 1943

As per Android Developer Guide (http://developer.android.com/guide/components/services.html#Basics) :

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

Upvotes: 2

Gangnus
Gangnus

Reputation: 24484

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.

Upvotes: 6

cistearns
cistearns

Reputation: 2508

Services are more analogous to a headless Activity.

The the important piece to understand is that a Service is about managing application lifetime and the ability to keep work running when your Application is not in the foreground (no UI visible). It is also about providing the ability to expose functionality to other apps.

http://developer.android.com/reference/android/app/Service.html#WhatIsAService

Typically when starting a Service you will also start a worker Thread. There are settings in the manifest that can cause a Service to be started in a new Process but generally you do not need to do this, it makes communication with your service more difficult.

Use a just Thread in your Activity when you need to offload work from the UI thread while the application is in the foreground, but this work can stop when you are no longer in the foreground. (It is possible that your app will continue to run when not it foreground but there is no guarantee depending on a number of factors) Generally speaking Android is free to kill your Activity if it is not in the foreground, and if your App process has no Activities or Services it can be killed.

Use a Service with a Thread to do work that will take place while your app is in the background and you want better guarantee about the lifetime.

Use a Service to expose non-UI functionality to other applications.

Upvotes: 4

dzeikei
dzeikei

Reputation: 2256

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.

Upvotes: 36

dmaxi
dmaxi

Reputation: 3297

Android Service don't run in a separate process (by default) and even don't run in a separate thread! It runs in the main thread(UI thread) of the application, therefore if you would like to do something time consuming task in the Service start a separate thread yourself, or use IntentService.

Upvotes: 2

Related Questions