Foreground Services with Notification Channel in Android

Mehmet Yozgatlı
Huawei Developers
Published in
3 min readDec 18, 2020
Foreground Service

In this article, I’ll talk about the foreground service on Android. After talking about the general information, I will share code samples over the project.

The official document mentions in Foreground Service as follows:

A foreground service performs some operation that is noticeable to the user. Foreground services must display a Notification. Foreground services continue running even when the user isn’t interacting with the app.

Examples of apps that would use foreground services include the following:

- A music player app that plays music in a foreground service. The notification might show the current song that is being played.

- A fitness app that records a user’s run in a foreground service, after receiving permission from the user. The notification might show the distance that the user has traveled during the current fitness session.

I will try to explain the logic of a part of the service class in an ongoing project. When the project is completely finished, we will share the codes as a reference app.

To create a Foreground Service, you need to do the following steps:

  • Start a Service
  • Display a notification about the foreground service
  • Once your notification is displayed, implement the logic for the Foreground Service.
  • Update the notification respectively. Note: Until the Foreground service is completely killed, notification has to be displayed.
  • Once the work is done, kill your Service.

We can now start by defining the Service class. If I explain the methods in this class:

Since I have implemented the LifecycleService class, I have to override the onStartCommand function. I created the working logic of Foreground Service in this function.

Afterwards, I wrote the codes about creating the notification and starting the service in the startForegroundService method.

To create a notification in the method, I made a check that the Android version must be Oreo and higher. When the notification configuration is complete, you can run the service using the startForeground method.

Since I tracked the time with the notification, it was necessary to ensure that the notification is constantly updated. That’s why I used the Observe construct.

I also wrote the updateNotificationTrackingState method for the “Resume” and “Pause” buttons and their functions in the notification.

I used the stopForeground function inside the killService method to terminate the service.

Service Class

How can we trigger the service within the activity? The answer to the question is as follows. Using these functions, we can manage start, pause, resume and stop processes.

Do not forget to make the necessary configurations for the service in the Manifest file.

Finally, I wanted to add the screen recording of the application for better understanding.

Sample Application Record

Conclusion

If you have such an interactive application in mind, you can try Android Foreground Service.

I hope it helped. See you in the next articles.

References:

--

--