Android Service Tutorial for Background Processes

Android Service Tutorial for Background Processes

What is Android Service

Starting with what basically is a service. A service is a component of an application that is capable of performing operations in the background without directly interacting with the user. Some component of application starts the service, which then continues to run in the background and keeps on running even if the user switches to another application or the application is destroyed. A service has no UI (User Interface). Some other component of the application can also bind the service, interacting with it and performing IPC (Inter Process Communication).
Examples of services are playing music in the background or handling transactions over the network.
This video practically explains an Android Service:


Now let’s get to the step by step procedure for creating a service.


First start by creating a new Android Studio Project:

Create New Android Project

Create New Android Project


Inside the activity_main.xml, create two buttons by dragging the button icon from the pallete. One button will be used to start the service and the other to stop. Name them accordingly. You can use the following xml code: activity_main.xml


The final layout should look something like this:

Front UI Android Service Example

Front UI Android Service Example

Use this code in the which will perform event handling by adding functionalities behind each button to start and stop the service respectively:


To begin creating a service, first create a subclass ‘MyService’ extending from the main class ‘Service’. Then declare the service in the AndroidManifest.xml file as follows:


Next we have to override some callback methods. A little explanation of these methods is given as follows:

  • onStartCommand()

The system will invoke this method by calling the startService(). Any activity can start this service. And once the service starts, it will continue to run in the background.

The system will invoke this method by calling bindService(). It is used only when you want to bind the service with an activity, i.e. enabling interaction between the service and activity so that the service returns something to the activity. However if you don’t want to implement binding, then you can return null on this method.

The system will invoke this method only at the start, when the service is first created.

The system will invoke this method when the service is no longer in use and is being destroyed. This method cleans up resources such as threads, receivers or registered listeners.


These methods have been overridden in the following code:

(This code will play the ringtone inside the onStartCommand() so the ringtone will start playing on the start of the service, until it is stopped).


The service also needs to be defined in the AndroidManifest.xml, as follows:



Modify the as shown in code. On tapping the Start Service button, the ringtone will start ringing and will continue doing so in the background until Stop Service button is tapped.

And you’re done! Hope that this article clearly explains you the Android Service.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

Buzzing your life from assorted Coding,tips and general life opinion. Made your life Buzz by BuzzMyCode articles, share with world.

What is Buzzmycode, we had started this website to give tech savy the new way of life by reading our fresh organic content on every technology and applications.

Copyright © 2017 SyncSaS. Theme by SyncSaS Themes, powered by BuzzMyCode.

To Top