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? Table of Contents1 What is Android Service?2 Android Service Example in Video3 Basics …

android service example

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x