Android Services, Types of Services, Service lifecycle, and Example

Android Services is the main component in android application development, its provide facilities to perfume long-running operation. By default Android services run on Main Thread (UI thread), for a run in the background you have to make it.

No UI is provided in an Android Services. Services can start form another component and run in the device even switching an application. Like downloading an app or play music.

Android Services, Types of Services , Service lifecycle and Examples

Android Services Types

1. Foreground

Foreground services are noticeable to the user and can see what happening or interact with the component. For example a Music player and Downloading file applications works. Foreground services continue running even users are not interacting or using it.

2. Background

Background service run in the background without user knows or user can’t see it. For an example Storing data and Sync data to the server. Some tasks do not need to show users what is happing in the background fo app.

Note: If your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself isn’t in the foreground. In most cases like this, your app should use a scheduled job instead.

“Official Android”

3. Bound

Bound service runs only as long as another application component bound to it. Multiple components can bind to the service at once, but if all of them unbind, the service will destroy. By calling,bindService() service is bind with the application component.

Caution: Android Services is running in MainThread , So If you service is going perform any blocking operation like Music play or downloading (networking work), you should create a new thread within the service. By separate thread you can prevent ANR ( Application Not Responding ) error and crashing.

Let’s build a Simple Android Service Example app :

In this example, we will build a very simple android service example. to see how you can stop and start service in android app.

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Now create a class “” and extend “Service” class

Implement must methods IBinder and override a onStartCommand(...) – > to start service,onDestroy() -> to stop service. You can think about it how to use in your apps.

Add the following code in  “

Step 3. Define service in AndroidManifest.xml file.

Here we have added <service.../> tag to include our service −

Step 4. Add two button in a main_activity.xml resource layout file

This button is for Start and Stop service in the app.

Step 5. Add the following code in

In MainActivity we are passing intent to startService(...) -> start service and stopService(...) -> stop service

Step 6. Run the application, in an emulator or On your Android device

Output screenshot Android service example :

Android Services , Services Types and Service lifecycle output

Download source code Android Service example :

Note : This example (Project) is developed in Android Studio 3.0.1 . Tested on Android 9 ( Android P), compile SDK version API 26: Android 8.0 (Oreo)



Coding in Java

Android Services callback methods & Description :


This method invoked when calling the startService(). Another component of the app can start this service like start from Activity. If you implement this, then you have to stop the service, when its work was completed by calling stopSelf() or stopService().


When another component wants to bind with service. In your app must be implement this method and provide interface to client can communicate with the service by returning an IBinder.You must always implement this method; however, if you don’t want to allow binding, you should return null.


The system calls this method when all clients have disconnected from a particular interface published by the service.


This method to perform one-time setup procedures when the service is initially created.But its calls either onStartCommand() or onBind() methods before onCreate(). Note , If service already running then onCreate() method is not called.


This method is invoked , when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, or receivers. The onDestroy() is the last call method , that the service receives.

Android Service Lifecycle :

Below diagram form google android site to show how Service lifecycle callback methods are invoked. You can implement this method to monitor changes in the service’s state and perform actioin at the time.

android service lifecycle

Note:  You are not required to call the superclass implementation of these all callback methods. Unlike the activity lifecycle callback methods invoked. The onCreate() and onDestroy() methods are called for all services, whether they’re created by startService() or bindService().

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.