Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 872
Cody Avatar asked Oct 28 '11 18:10

Cody


People also ask

Why do we need thread in Android?

The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit. To keep your application responsive, it is essential to avoid using the main thread to perform any operation that may end up keeping it blocked.

What is the difference between thread and service 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.

How you choose between a service and a thread?

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.

Why is it important to put long running code in a service on a separate thread?

By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.


2 Answers

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.

like image 50
JimmyB Avatar answered Sep 26 '22 16:09

JimmyB


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.

like image 41
Patrick Kafka Avatar answered Sep 22 '22 16:09

Patrick Kafka