43

I was going through Bound Service in Android Developer website. I thought I understood the service enough but I just found another way of connecting service through Using a Messenger class especially for local service. There I got confused. Maybe I got the concept wrong.

Here is my understanding of Android Service. You create a service when

  1. You want to do separate jobs in the background.
  2. You want to make it a separate process.
  3. You want to make it run in a lifecycle that's independent of the component that started it.

Confusion is the first item in the list, the definition of the background. Isn't the background a thread or process? I never thought that it can run on the main thread.

Here is the caution of service in the dev pages about.

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. 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.

Questions

  1. Why does one choose to use service if the service function will anyway run on the main thread?
  2. Do we have to write a service only to block ANR even if the time-consuming job is done in the main thread? Assume the service is only for my application.
  3. Are there any practical cases or reasons to use a service as private and running in the same thread?
Master Fathi
  • 349
  • 1
  • 9
  • 19
Jaekwan
  • 453
  • 1
  • 4
  • 7
  • 1
    the main purpose of services is that they have different life cycle than your activity life cycle - so #3 applies here. – pskink May 19 '15 at 05:45

3 Answers3

52

Application main thread is not always the UI thread. For example, when Activity is stopped, the onStop() is invoked, hence the UI thread is taken away from that Activity and moved to another Activity within the same or a different application. However it doesn't mean the application is no longer active, it can continue working in the background until it is closed either by OS or by user. Then who keeps it running in the background? It is the main thread and not the UI thread.

What are services

In Android, a Service is an application component that can perform long-running operations in the background on the UI thread. By background, it means that it doesn’t have a user interface. A Service runs on the main thread of the calling Component’s process by default (and hence can degrade responsiveness and cause ANRs), hence you should create a new Thread to perform long running operations. A Service can also be made to run in a completely different process.

Unlike Activity components, Services do not have any graphical interfaces. Also Broadcast Receivers are for receiving broadcast messages (broadcast, multicast, unicast) and perform short tasks whereas Services are meant to do lengthy processing like streaming music, network transactions, file I/O, interact with databases, etc. When a Service is started by an application component like an Activity it runs in the background and keeps running even if the user switches to another application or the starting component is itself destroyed

Why use service

Services are given higher priority than other Background processes and hence it’s less likely that Android will terminate it. Although it can be configured to restart once there is ample resources available again. You should go through the different processes and their priority/important level in the documentation on processes and threads. Assigning them the same priority as foreground activities is definitely possible in which case it’ll need to have a visible notification active (generally used for Services playing music).

Use IntentService if you don't want to fiddle with managing threads on your own. Otherwise, use AsyncTasks.

Please read this excellent article to understand more in detail and also read this answer.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Sam
  • 1,237
  • 1
  • 16
  • 29
  • I got the point I think. They are basically different purposes(Acitivity only for short context(a screen) and service is broader) and not useful to distinguish them in terms of the performance since the service runs on the main thread anyway. – Jaekwan May 21 '15 at 16:07
  • 3
    According to https://stackoverflow.com/questions/3261370/is-main-thread-the-same-as-ui-thread main thread and ui thread are same – 10101010 Oct 26 '17 at 18:53
  • I totally disagree with your statement that service runs on UI Thread, Kindly share any official link saying that Service runs on UI Thread. Following is the official link of Android Service and it does not say anywhere that Service runs on UI Thread. https://developer.android.com/guide/components/services – Shaz Hemani Jun 08 '18 at 08:32
  • 5
    I would remove the entire first paragraph; it is inaccurate. For Android user-mode apps, thread "main" _is_ the UI thread. Threads do not "switch" between different apps. Processor cores may switch to different processes/threads, but that is something different. A user-mode thread cannot "move to" another process. All `Service` and `Activity` lifecycle callbacks will happen on this one, unique, main/UI thread (assuming, of course, we're talking about an in-process `Service`). – greeble31 Dec 27 '18 at 20:10
3

Service basically runs in UI thread or main thread.But,if we are going to perform long running operations in service,we need to create a background thread and perform that task.

But why we have to use service?

Now let's think of Music Application.We need songs to be played continuously even if we leave music app.If we use activities,we can't achieve above requirement.So,service helps in these kind of scenarios.Even if the app is not in foreground, service keeps on running and we are able to listen to songs.This is why we use service even though it runs on main thread.

Tarun Anchala
  • 2,232
  • 16
  • 15
-3

In short, Services runs on the background of the UI thread. You can perform tasks like client-server authentication or write to a database where the tasks are done in the background with no graphical interface.

But if you're doing a really long processing tasks that could freeze the interface, you use a service on a separate thread.

eg of a Service on a separate thread is IntentService

anto004
  • 287
  • 2
  • 6