Bound And Foreground Services in Android, A step by step guide.

In Android applications sometimes we often need that Activities/Other apps can communicate with each other. For this medium, we can use ‘Bound services’. It is like a server in a client-server architecture where there is several clients requesting something from the server and server responds them back with the required result.
Use Case: A music player where a music is playing inside a service and an activity is requesting progress and handling controls by binding to the service.
Now, let’s understand the components used in the demo.
MyBoundService: This is the actual service class which will be extending the Android framework’s ‘Service’ class.
MainActivity: This is the initial activity which will be used to start and stop the service, Also this is the class which will bind the service too.
ResultActivity: This is the second activity which will also bound to our already running service to show how multiple clients can connect to the same running service.
Now we will see how to connect these components all together.
First, create an inner Class ‘MyBinder’ which extends the ‘Binder’ class.
This will return the instance of our service class. Which we will be using later.
Now create these variable with the global scope so we can use them anywhere in our activity.
- ‘mBinder’ variable holds the Instance of our Binder.
- ‘mGenerator’ holds an Instance of Random class which is used to generate Random number.
- ‘randomNumberLiveData’ is used for creating LiveData that will hold our Randomly generated number.
- ‘CHANNEL_ID’ is used for creating a Notification channel used by our service.
Now we will override ‘onBind’ method, This will return the instance of IBinder class.
Now, We will create a method called ‘startNotification’ which will be used to create a Notification that is used by our Service(Foreground service) class.
Now, override ‘onCreate’ method, which will be invoked when our service will be created. In this method, we will create a Notification for our service class and also we will generate a random number and post it to the live data. So our connected clients can listen to that.
So, this is all about our service class, Now we will see how can bind to this service class from our Activity class.
Now, we will create an Activity called ‘MainActivity’ following will be the UI for the same.

We will create 2 variables ‘mService’ to store an instance of our ‘Service’ class and a boolean ‘mIsBound’ to check if our activity has bound to our service class or not.
Also, we will create a method named ‘getRandomNumberFromService’ which will be used to get an instance of our ‘LiveData’ object which was previously created in our service class for getting the value of the random number from our service class.
We will also create a variable named ‘serviceConnection’ which will be implementing ‘ServiceConnection’ interface. This will be used for binding to our service class.
We will override 2 methods from this interface named ‘onServiceConnected’ (This method will be called when our service will be connected to our activity) and ‘onServiceDisconnected’ (This method will be called when our service will be disconnected from our activity).
Also, inside ‘onServiceConnected’ we will get an instance of ‘IBinder’ class, which is used to get the Instance of our service class so we can communicate with our service class from our activity. For getting an instance of our service class we can use the method ‘.getService()’ from the ‘binding’ object it gave. We will also call our previously created method ‘getRandomNumberFromService’.
Now, we will create 2 methods ‘bindService’ and ‘unbindService’ method. These methods will be used for binding and unbinding to our service class.
Inside these methods, we will be passing an instance of ‘serviceConnection’ Interface to the ‘bindService’ and ‘unbindService’ methods.
Also, we will be overriding ‘onDestroy’ method in our Activity class and we will unbind to the service in it. So it stops as soon as Activity is destroyed.
Now as seen in the UI. We will be implementing click listener’s of all three buttons ‘Start Service’, ‘Stop Service’, ‘Start Activity’. This will be the click listener for all these buttons.
Here we will be binding the service to our Activity class on click of ‘Start Service’ button, We will print the result received after binding the service into a ‘TextView’, We will unbind from the service on click of ‘Stop Service’ button. We will start another ‘Result Activity’ on click of ‘Start Activity’ button.
After binding to the service we can see the result get printed into the ‘TextView’ like this.

Now, we will create another Activity named ‘ResultActivity’. We will replicate all the previous steps as done in ‘MainActivity’. And we will bind to the service in the ‘onStart()’ method of ‘ResultActivity’.
So as soon as we started ‘ResultActivity’ and our activity binds the service class. It will print the result from the service class into our Activity class.

Here we can see that the result received in both activities are same i.e ‘13’.
So in this way, we can bind/unbind to a service from our activity and can interact with it directly.
One example is like our service is used for playing audio and our Activity wants to listen to the current position of music and control the play pause etc. So in such case, we need to create a Foreground service and can bind our Activity to the Service class.
Some points to remember
- When any client bind to the Service and another client request to bind the same service. It will get the instance of service created previously and our service class will not be restarted or anything.
- When all the clients unbind from the service. The service will be stopped automatically.
- If a foreground service is started using ‘startService’ method. then it will run indefinitely in the background until we call ‘stopService’ method even if all clients unbind from the service.
Source Code of project: https://github.com/iambaljeet/BoundServiceDemo
That’s all folks. If you liked this tutorial. Just SHARE and CLAP. Thanks.