Communication Between Service and Activity in Android

Posted By on March 23, 2016

Download PDF
Android Service
Methods in OOPS

Communication Between Service and Activity in Android (2 Way communication)

After posted a complete tutorial on simple Android service with example. Now it is time to learn the real usage of Android service in  an Android application. So here we will discuss on the communication between service and activity in Android (startService, stopService onStartCommand etc). So lets going to the coding part e.g , how to communicate between Android Service and Android Activity. If you are new to Android services then first you should look into my article on ‘Simple Android service example code with description’ . 

  • What we will learn here using Android Service Example Code

  1. How to call an Android service from an Activity.

  2. How to communicate between android service and activity.

  3. How to manage Android service lifecycle

  4. How to use a thread in side Android service

  5. Create an Android Handler for communicating with an Activity.

android service example start service android service example send message

As we know the lifecycle of an Android Service is different from an Android Activity. If you are interested to know about the life cycle of an Android Service, then click here. Here will create a service, then start it from an activity. After that we will sending and getting messages between the service and activity.

 Create Project: Android Service Example

1. Create a new project with project name: ServiceExample

2. Fill Application name: ServiceExample

3. Fill Package Name as: com.techblogon.serviceexample

4. I have used SDK version Android 4.0.3 and Eclipse Version Indigo. But you can use any version.

 5. Add below code in the (activity_main.xml) file in your project’s res/layout folder. This contains 3 buttons and a simple textview on the GUI.

 6. Register your service class in the Manifest file(AndroidManifest)


7. Now add a new java file (MyService.Java) for your  Service Class code to your projects /src folder or just copy and paste the below content.

Here We have created a new class called MySercive which is extended from Android inbuilt Service class. Then in that class we have @ Override some required virtual functions like ( onBind(), onCreate(), onStart(), onDestroy(),  onStartCommand etc).


 Note: Code Description for above MyService Class

  • We have @Override Service Class functions () in the above code.
  • Once you @Override onStartCommand() function in the code as above. Then when you will callonStartService() function from your activity, the onStartCommand() function will be called followed by onCreate() command.
  • We have Create a Thread Class and start the thread from the onStartCommand() function. Which will help us to do long operations without making the GUI unresponsive.
  • In the Thread class we implemented the run() function and in that run() function use the Looperclass to get messages from the Activity at any particular time. You can find some code related to Android Handler functionality. We have used Handler functionality in the above code to respond all incoming messages from the Activity.
  • Sending Message to Activity Class. When we want to send message to an Activity from a service using Android handler, what we need is:  the handler instance of the activity class. You have to implement the handler code in your Activity class to handle incoming request from other components. We have implemented the below code in our MyService Class as above.

 8. Add below code in your default file.  We have 3 buttons for ‘Start Service’,‘Stop Service’ and ‘Send Message’ in the Activity class. We will start the service by clicking the button ‘Start Service’, stop the service by clicking the button ‘Stop Service’ and Send Message to the Service and get the Response from the service by Clicking the button ‘Send Message’. Then we will show all status by displaying Android Toast message.


Notes: Code Description for above MainActivity Class

  • Adding handler code for processing incoming messages from the service class. We have added handler code inside the onCreate() method of the MainActivity class as below. then show the message in a Android Toast message.
  •  Make the handler instance as public static, so that other components (service in our case) can access to send messages to Activity.
  •  Sending Message to Service Class. We just retrieve the Service class handler class’s Handleinstance. then send message using sendMessage() function as below. You must implement the service handler as public static, so that other components can access it to send messages.
  •  Start Service.  When you click on the ‘Start Service’ button, then the service will be start.
  • Stop Service. When you click on the ‘Stop Service’ button, then the running service will be stop.
  • Send Message. When you click on the ‘Send Message’ button, then the message will be send and get the response from the service. Then we will show the response using Android Toast message.

9. Build and Run the project. Watch the behavior of your Android service Example.

Android Service
Methods in OOPS

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby