Android Service Example for Background Processes

Hey guys, in this post we will see a simple Android Service Example. But first lets understand what is an Android Service?

What is Android Service?

Service is a process, but the special thing is about the service is it doesn’t need user interaction and it runs on background. I hope you can imagine some Android Services Examples now. Like Playing music in background. It is a long running process and it does not need user interaction. In this Android Service Example I will show you playing music in background.

Android Service Example in Video

  • You can watch the following video to get a detailed explanation as well.

Basics of an Android Service

Creating a Service

To create service we will create a normal class extending the class Service. And we should override the following methods.

onStartCommand()

  • This method is invoked when the service is started using the startService() method. We can call the method startService() from any activity and it will request the service to start.

onBind()

  • If it is needed to bind the service with an activity this method is called. The service can result back something to the activity after binding. But if you do not want to bind the service with activity then you should return null on this method.

onCreate()

  • This method is called when the service is created.

onDestroy()

  • When the service is no longer used and destroyed this method is called by the system.

Defining it on Manifest

  • You need to define your service in your AndroidManifest.xml file. It is very important.

  • You need to put the above code inside application tag for every service.

Android Service Example

Now lets see a working Android Service Example.

Creating an Android Studio Project

  • Again create a new android studio project.

android service example

Creating User Interface

  • Once the project is loaded come inside activity_main.xml and create the following layout.

android service example

  • For the above UI you can use the following xml code.

  • Now we will code the MainActivity.java as below.

  • Now we will create our service.

Creating Service

  • To create a service you just need to create a new class extending Service. And also you need to override some methods.
  • I have create the following class MyService.java.

  • As you can see we are simply playing the default ringtone inside the onStartCommand() so when you start this service the default ringtone will start ringing on loop until you don’t stop the service.

Defining Service in Manifest

  • Before starting the service we need to define it inside AndroidManifest.xml, so modify manifest file as below.

  • Now lets see how we can start and stop the service.

Starting and Stopping Service

  • Again come inside MainActivity.java and modify it as below.

  • Now you can run your project. When you will tap on Start Service ringtone will start ringing. And even if you close your application ringtone will keep ringing. Because it is playing with a Service that runs on background. To stop it you have to stop the service using Stop Service button.

So thats all for this Android Service Example guys. Please leave out your comments if having any queries or confusions. Thank You 🙂

19 thoughts on “Android Service Example for Background Processes”

  1. use this START_NOT_STICKY instead of START_STICKY in the MyService.java file to avoid restart of the music even after the app gets terminated completely

    Reply
  2. I am trying to set a countdown timer in recyclerview as an item, but that problem is when I am moving to other page this time is starting from first. How can I get update time, Can you please help me

    Reply
  3. hi, I’m new to android development. will you please make a tutorial on how to send https GET request in the background (even if app closed) and show the data on notification or at least detailed example. I’m trying to do this for a while now, please?

    Reply
  4. Thank you . Its working

    One more Doubt :

    How to check internet connection through service.
    Eg : WiFi or Mobile Data is Connected but No Internet at present at the time or manage speed of the internet.

    Reply
  5. Hi, thanks for the explanation.

    My question is why the app keeps stopping when the start button is pressed? (the music is playing ok)

    Thanks again.

    Reply
  6. Working fine ! but when i close the app from RECENT APPS it will going stop sound. How do i resolve this ?
    I’am using android nougat version.
    Thanks.

    Reply

Leave a Comment