service in android

What is Service in Android?

Service is nothing but a ghost that exist but we can not see. In relation to Android, service is an application component that can perform long-running operations without having a user interface.

Not going to some jargon we explain you service in very easy way

Did you ever play music in music player, Then you see after closing music player in the notification panel there is a notification continuously showing that which music is currently playing

Yes, This is done using a service. Service can handle many things like network transaction, playing music, file I/O, etc.

Types of Service in Android

In Android there are three type of service

Foreground Service

Foreground service is the type of service that is visible to the user and the user is notified that the particular service is running. Just like Music Player but in foreground service you must have to show a notification to the user that service is running in the foreground

Background Service

Background service is the type of service that is not visible to the user and there is no need to notify that the particular service is running.

Bound Service

When an application component bind to a service it is known as Bound Service. A bound service run as over another application bind to it when another application unbind it Bound service are destroyed

How to start a service in android

If you want to create a service then there is 2 options

  1. Create by going to File-> New -> Service -> Service
service in android

2. By creating a java class and then extends Service and override some method’s

In the First option, you don’t need to declare service is manifest because android studio automatically add it but using the second option you manually need to declare in the android studio like this

<manifest ... >
  <application ... >
      <service android:name=".ExampleService" />

These are some important method which you need to know before creating service


This method called by using startService() in activity or fragment whenever you need to start a service and after service start it is user responsibility to destroy the service otherwise service will run indefinitely


This method called by using bindService() when another component want to bind with service


To destroy you need to call this method.

Creating a Background Service Practical

First, we need to create a Service by going through File->new> new> service and named as GeeksToCode.class

public class MyService extends Service {  
    public IBinder onBind(Intent intent) {  
        return null;  
    public void onStartCommand() {  
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();    
    public void onDestroy() {  
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();  

In Activity_main.xml

android:text = "Start Service"/>
android:text = "Stop Service"/>


Button button1,button2;
Intent intent;
    protected void onCreate(Bundle savedInstanceState) {
       button = findViewById(;
       button1.setOnClickListener(new View.OnClickListener
          intent  = new Intent(this, GeeksToCode.class);

 button2.setOnClickListener(new View.OnClickListener



When you run this app and click on start button you will see a Toast indicating Service is started


Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

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