Cody
Cody

Reputation: 8944

When to use a thread / service in Android?

When should a thread or a service be used?

Should they be used for authentication? For instance, in my app I was considering using a thread or service (I am authenticating via Active Directory.)

Do you have examples of when each would be used?

Upvotes: 30

Views: 18772

Answers (6)

Patrick Kafka
Patrick Kafka

Reputation: 9892

A thread should be used in a long running process that would block the UI from updating. If it's more than a second or two you might want to put it into a background thread and notify the user with a dialog or spinner or something. If you lock the UI thread for more than 5 seconds the user will be prompted with a "kill or wait" option by the OS.

A service does not run on separate thread, so it will block the UI, but you can spawn a new thread within a service. A service is used more for something that should happen on an interval or keep running/checking for something when there is no UI shown.

Upvotes: 29

JimmyB
JimmyB

Reputation: 12610

Update: It seems the Android documentation includes a corresponding clarification now, see http://developer.android.com/reference/android/app/Service.html#WhatIsAService.

Original answer:

In Android, a Service does not provide any concurrent execution ("run in background"). It is actually more of a simple Java object which merely is instantiated (and managed) via the Android system instead of your application via new.

The most important property of a service is therefore not about deferring workload; this can be achieved with simple threads.

What makes a service object special is that it is registered with the Android system as a service. This let's the system know that this object provides some sort of service and should be kept alive as long as possible, or until it is stopped. Normal application threads do not have this special meaning to the Android system and will be terminated much more generously at the discretion of the system.

So, if you need some background activities to go on only while your application/Activity is active, a thread can do what you need.

If you need a component that keeps active will not be purged even when, after a while, the Android system decides to remove your Activities from memory, you should go for the service, or even a "foreground service", which is deemed even more important by the system and even less likely to be terminated to reclaim resources.

Of course, if desired, a Service object can also be made to contain one or more Thread instances which could then live as long as the Service object itself.

Edit:

Oh, plus: A service is, of course, the way to go if you want to provide some service(s) to other applications, which can "bind" to a service only.

Upvotes: 50

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: 1

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, 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.

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.

Upvotes: 1

Alex Gitelman
Alex Gitelman

Reputation: 24722

Use service if you need something that is either used by other applications or outlives your application activities. The good example of service is file transfer that may take long time and you don't want to force user using your application during this time. Use thread (usually via AsyncTask or similar) in other cases.

For authentication purposes AsyncTask seems like a good choice.

Upvotes: 3

Related Questions