Why use Service if it runs in the same thread in android

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.


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.