The service is one of the most used components in the Android operating system. The service component is used to execute long-term and repetitive operations and processes in the background. Processes that have nothing to do with the user interface (UI) and must be done away from the user’s eyes. In this article, we will explain the concept of service in Android, its types, and its usage.
Table of Contents
What is the service?
Service is a software component that runs in the background and does not directly interact with the user. Since the service does not have a user interface, it is naturally not connected to the life cycle of an activity.
Services are often used to perform repetitive and lengthy operations. Among these operations, we can mention downloading from the Internet, checking and searching for new data, processing information, updating content providers, and similar things.
Services have a higher level of priority than inactive/invisible activities in the UI, and hence the probability that Android will terminate them automatically is very low.
Android allows you to set the services in such a way that if it is forced to remove these services from the memory for any reason, it will be able to restart them as soon as sufficient resources are available to the system.
Services can be assigned the same priority as the ACTIVITIES in the foreground. In this scenario, it is necessary to include a visible and active notification in the UI for the relevant services. This method is mostly used for services that play a video or music file.
Services and background processing
By default, the service runs in the same process as the main thread of the application. For this reason, the developer should use asynchronous processing in the service and start tasks that are expensive and heavy in the background. One of the frequently used patterns for service implementation is running a new Thread in the service to perform background processing and terminating the service when the processing is finished.
The services that run within the process of the application itself are usually known as local services.
The services of the Android environment (platform) and dedicated services
The Android environment considers and launches pre-defined services that all Android applications can use if they have the necessary permissions. System services are provided by a class called Manager to the applications. It is enough to call the getSystemService() method to access it.
The Context class provides several constants that you can use to call the named services.
In addition to the default services of the Android system, the Android application can define specific services and use them alongside the system services.
The developer can design responsive and interactive applications by implementing his services. You can fetch the data of the application using the service and provide new data to the user when the application is launched.
Setting up and defining dedicated services
Dedicated services are often launched by other components, in other words, other software components of Android applications such as activities, broadcast receivers, and other services launch dedicated services.
Foreground services
The background service is a service that is the same in terms of priority and importance as an active and visible activity in the UI, and therefore, even if the Android system is facing a lack of memory, it is still not allowed to remove the memory from them. The foreground service should have its notification under the title “Ongoing” in the status bar. This means that until the service is removed from the foreground or memory, the notification cannot be dismissed and removed from the status bar.
Definition of dedicated services
- Implementation and notification
- The process of setting up and running the service
- Stop a service
- Two-way connection of services (service binding)
- Connecting activities to services
- Connect to local services
- Connect to the service using IPC
- Running services in separate processes
- Running a service in its process
- Data exchange and communication with services
Different ways to communicate with the Services
There are different ways to exchange data and interact between activity and service. The following content mentions the possible methods to achieve this goal and provides you with the proposed method.
Using data encapsulated in intent
In a simple scenario, there is no need for any direct interaction (between service and activity). The service receives the data encapsulated in the intent from the initiating component (service caller) and performs the necessary operations. It should be noted that notification is not necessary for this purpose. When the content service of a content provider updates with new data, the software component itself informs the activity about this event, and no other action or step is required in the service. This method can be implemented and used both for local services and for services that run in their process.
Using the receiver
Broadcasts and receivers that listen to these broadcasts can be used for interaction and communication between activities and services. For example, your activity can register a broadcast receiver to listen to a certain event and the desired service will announce the occurrence of the relevant events to the outside (other components). This method is very common and is often used when the service must notify the activity after completing the processing.
The mentioned method can be used for local services and services that run in their host process.
Connecting the activity to the local service
If both the activity and the service are executed in the same process, then the activity will be able to connect to the service directly in a two-way manner. The current method is the most optimal among the options mentioned so far and is very suitable for when the activity needs to exchange data with the service at a high speed.
This method can only be implemented and used for local services.
Handler and ResultReceiver or Messenger
If the service has a two-way interaction with the activity (returns information to the activity), then it can receive a Messenger object through the data encapsulated in the intent that it receives from the activity. If the Messenger is connected to the Handler in the activity, then the service will be able to send Message objects to the activity.
Messenger implements the parcel-able interface, which means that it can be sent to another process and send messages to the Handler in the activity using this object.
Messenger also provides a method called getBinder. This method provides the ability to send a Messenger object to the activity. The activity will subsequently be able to send multiple messages (instances of the Message class that contain descriptions and arbitrary data objects) to the desired service.
This method can be used for local services that run in their process.
Connect to a service in another process using AIDL
To exchange data and connect (bind) to a service that is running in another process, the programmer must get help from IPC (inter-process communication). To achieve this goal, it is first necessary to create an AIDL file, which is almost similar to Java interfaces, with the difference that its extension is .aidl, and it is only allowed to inherit and expand other AIDL files.
It is recommended to use this method when you need to connect to a service that is running in another process. For example, this approach should be used when other applications request to use the desired service.