Android & Kotlin

Mastering in Android Service

Pinterest LinkedIn Tumblr

What is Service

When none of applications is active state than few Android components are being active in the background and that active component is actually the service. In other words, Service is an application component that executes task in the background without affecting user’s current activity. By default, Service always runs in the main UI thread, It does not runs in separate worker thread and also it is not run in a separate process.

Let’s take some example for better understand. Suppose a user is playing his game, This game is basically activity that shows the current interaction of the user and sometimes while playing the game in the background there are some operations that are going on.

For example while user is busy playing the game, there are few applications are getting updated from Play Store and we all have faced this situation where while interacting with any application in the background all of a sudden the application starts to get updated from the Google Play Store. While the other applications are being updated in the background that time operation is not affecting the current user interaction of playing the game. So all of these background operations actually carried out by the Android component known as a service

Understand the Android Service

For better clarity let me show you one more example. Suppose you are using the Facebook application and while using the Facebook application there are few background operations that are going on such as uploading or downloading of a file from some other application or while using the Facebook application you want to listen to the music. So here again these two operations are actually being carried out with the help of the Android component known as Service.
Now whenever background operations going on then all the background operations consume some memory. So other words you can say, whenever we use any application or perform any task it definitely eats up your device RAM.

Type of Service

In my opinion the service is broadly categorised into two types the first one is the Started Service and the second one is the Bound Service. The started service can be further categorised into two subcategory the first one can be called as the Foreground Service and the second one is the Background Service .

Now if you have a look at the DEVELOPER.ANDROID that is the official documentation of service then you will find that there are three types of service such as the Foreground, Background, and Bound Service but right now I thought it is much better to categorize the service in 2 subcategories of Started Service and the Bound Service

1. Started Service

Started Services is started with the help of an Android component(Activity, BroadcastReceiver, ContentProvider and Service). That why it is known as a Started Service. The Started service can be started by the Activity, BroadcastReceiver, ContentProvider and the Service itself. Here is noticeable point is that a service can start another service as well as.

The Started Service is starts with the help of a method of startService() method. Whenever any Android component a Service the Service runs indefinitely in the background and even if the calling component is destroyed then also the started service will continue to run indefinitely in the background. Basically, I am trying to say is suppose the calling Android component is destroyed then also the started service continues to run and this might leads to memory leakage. The started service performs a single operation at a time and there is always a one way communication.

In case of started service that is we can pass the data from the Android component to the service but by default there is no mechanism to get back result from the service back to the calling component so we have to find a workaround to get back result and as said a service continues to run indefinitely in the background that why we should always call stopSelf() method or stopService() method to stop the service and avoid any memory leakage.

Let’s summarise Started Service in short
  • It started by Android components, these components are Activity, ContentProvider, BroadcastReceiver and Service itself.
  • It stared with help of startService() methods and stop by stopService() or stopSelf() methods.
  • When service once started its run in the background indefinitely even when the stared component is also destroyed.
  • By default it has one-way communication only.
  • We should stop the service after the task is finished by using stopService() or stopSelf() methods

2. Bound Service

The service is called a bound service when Android component simply to the particular service. We can bind service with three component. The components are Activity, ContentProvider and the Service itself. BroadcastReceiver can never bind the service. and binding to the service is achieved with the help of a special method of bindService() methods. A service as long as there is at least one component bound to it.

When this component is destroyed then the bound service is also destroyed simultaneously. So we can say that when all the calling components are destroyed then the service is also destroyed. In other words you can say, The Bound Service is dependent on the bounded component for its (own) existence. The bound service has a two way communication Unlike the started service where we had only one way communication so here in case of bound service we have the two way communication you can pass data from the component to the service and also by default get back result.

Summery of Bound Service
  • We can bind to the service with Activity, Content Provider and Service components with help of the bindService() method.
  • When all calling components are destroyed, the service also destroyed.
  • Bound Service continuously interacts with calling components.
  • By default is have two way communication.
1.1 What is Foreground Service

Suppose you are currently using the Gmail application and listening to the music that is being played by some other application. So music player is basically using the foreground service to play the music. The foreground service always uses the notification to notify the user that some long running operation is going on and using the notification you can actually interact with the service or the ongoing operation such as pause the music or play the next music.

So whenever in your application you see a notification that is performing some long running tasks that that service is basically the foreground service and the foreground service is always noticeable to the user that is the user is aware of this ongoing process.

Foreground Service is actually created by using the startForeground() method. So when we use this method it actually the service to run in the foreground. And by using the () method the service again returns back to run in the background and now its becoming the background service now.

I point is noticeable here that using stopForeground() method foreground service is not destroyed. Basically, it actually makes a foreground service to background service. So how to destroy the foreground service so , that again you need to call the stopService() or stopSelf() the method to destroy the foreground service as well.

When a service runs in the foreground it should always be noticeable by the user to always use notification for the ongoing operation like we saw in case of music player application or downloading or uploading of the file or while updating the applications from the PlayStore we also have the notification to notify the user that some operation is going on.

1.2 What is background service

I will take example of the WhatsApp application and also the Gmail application by taking these two examples you can actually to correlated the things with the Facebook application as well.

Let take a example

Suppose you are connected to internet, Now you will simply turn off your Wi-Fi right. In case you have no internet on your phone. Means you are offline. Now let try to send some message to your friends WhatsApp account. So here you will just try to send a message like (Test message). After sending the message you are not connected to the internet that why this message is currently undelivered and it is showing a pending status close the application and let also destroy the WhatsApp application from the minimized application list. So right now I don’t have any running application and no recent items in the recent app.

Now just turn on Wi-Fi. Now device is connected with internet and message is automatically delivered without WhatsApp app open. In other words WhatsApp application is still closed you have not opened my WhatsApp application but still, my message was delivered to your friend’s device. how is that possible? This is simply of background service. The WhatsApp application was actually running the background service and due to that service, my message was delivered in the background itself so we don’t need to open the WhatsApp application for our message to get delivered so this is the beauty of using the background service.

Conclusion

A service is started it is actually background in nature so when we use in the startService() method it actually creates a background service by default. Now starting from Android Oreo that is API 26 onwards there are certain restrictions imposed on the background service.

I will discuss it in my upcoming articles so please don’t worry about it and next to the background service is not noticeable by the user as we saw in case of WhatsApp and Gmail how they use the background service to send data to some other user in the background.

Write A Comment