Android Services

Hello, everyone. We’ll talk about android services in this article.

Services are one of the basic features in android. When you develop an android application, services play an important role. Thus, you should learn the services as well as you can. We will talk about services in detail in this article.

A service is an android application component that can perform long-running operations in the background. Service is also an android component like the other android components but it works in the background.

Another feature of the services is that there is no user interface. Services do not need a user interface. Because services do not interact with users.  Normally other android components are in interaction with users. Such as mail input, password input, click some buttons, etc. But services do not need this.

A service can be started by another android components. For example, when you click a button in the android application, a service can be started in the background by this button.

If I want to give a real-world example, I can give a file download example. In the application, when you click a download button, a service starts to download the file in the background. Some times, this file can be a huge size and it can take a while to download this file. A service takes this download task and does it in the background. Meanwhile, you can continue working in the application.

In this download example;

First, download works in the background.

Second, this download service does not have a user interface. It can show the user a download bar or a notification. But these are not in the activity or in the application.

Third, this service started with a download button. That means, service component started with another button component.

We can give another service example. Music player application can be an example for services. A music player can continue to play selected music in the background.

Another example could be something like this. An application can download a photo from the internet in the background.

Or an application can send information to the server in the background.

You can give a lot more examples of services. There are many extensive uses of services in android applications.

There are three different types of services in android applications.

Foreground Services

In the foreground services, the user can see the process. For example, your application will download a file in the background. This is a foreground service. In the foreground service, the user needs to see notifications. Also, the user needs to see the download process. Or, when the download finishes, should be given information to the user.

Also, even though the user does not interact with the application, the service has to continue to work in the background. For example, your application starts to download a file in the background. This file could be a huge size. Let’s say one gigabyte. While the download process continuing, the user can continue to work with the application. Even the user left the application, download process must continue.

Background Services

In the background services, the user does not see the process. The application does not show the notifications to the user. Some times, the user even knows nothing about the service process. You can do everything in the background.

For example, you develop an application. This application stores some photos in the device memory. While application storing photos in the device, it can compress photos in the background. The user does not have to know this compress process. What application does in the background or how application compress photos in the background. The user does not know anything about these processes. What does the user do? The user just takes the photos and save them in the device. While you store these photos in the background, you can create a compression service in the background so that the photos take less space on the memory to make your application work better on the phone.

You can compress photos in the background and when you need these photos, you can uncompress them again. In this service, you are doing some operations in the background and without the user’s knowledge. Also, the user does not interact with this service.

Bound Services

In this service, service is dependent on the component that calls itself. Service interacts with the component permanently that calls itself. when the component that calls itself stops, service also stops. Service does not continue to work in the background. In the literature, this relationship is called client-server relation. In this relationship, the server is service and the client is a component that starts the service.

Music player application can be an example for the bound services. When you start a music player application, service sends data to the player periodically. This data could be which music is playing or what is the name of the next music or which minute of music is playing and etc. When the user closes the application, also music should stop. When you start the application again, music also can start to play again. You see that service is in continuous interaction with the component. We call this kind of service, bound service.

When you come to the service writing part, there are two service classes.

The first one is the standard service class.

So, what is the standard service class? This class is a traditional service class. This service class takes on the whole task on its own. But this service class has a feature that uses the main thread of the application.

I think I need to give some details in this part. The Android operating system gives each application a share of the processor. That means, your application uses this share of the processor. Also, your service will use the same share of the processor. if the service uses most of this share, your application will slow down. Because of this, if your service has a big task, you should not use this standard service class. Because it will slow down your application. But if your service task is small, you can use this standard service class.

For example, your application will download a movie from the internet. At the same time, you want to use your application. the download process will take time and will use resources of the application. When you use the application at the same time, it will work slowly. The users do not want a slow application. you need to solve this problem.

In this part, another service class helps us. It is the intent service class.

The intent service class is a subclass of the service class. The intent service class does not use the application main thread. It creates another work thread and uses its own thread. That means intent class requests another share of the processor from the android operating system. This does not slow down the application. The intent service class is used in long-term background processes.

So, that’s it. This all was about Android Services. See you in the next article.

Written by Kadir Yapar

I am a software developer with a Master's degree in Computer Engineering and over five years of experience in the field. My expertise spans Android, iOS, and web development, and I have a track record of developing and publishing applications for these platforms. I am dedicated to creating high-quality, user-centric software solutions and continually stay updated with the latest industry trends. My ability to collaborate effectively in cross-functional teams and adapt to emerging technologies makes me a valuable asset in the software development landscape.