Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Handle Foreground Services in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Regardless of the target SDK version, users can perform a workflow from the notification drawer to terminate an app that is running foreground services as of Android 13 (API level 33). Foreground Services (FGS) Task Manager is a newly introduced API in Android 13 that lets you put your system on a check to see if your system is being run after heavy usage or if app services are in the foreground. You may use this to monitor what is not letting your system perform in the optimum conditions. Using this API you can have a thorough check of all the foreground services which are running currently on your system, however, some of these services are considered important by the Android system and hence have made their way to the exceptions list. In this article, we will be knowing what the hype about FGS is all about, and how to use this while developing your Android Application, so that you can avoid getting caught in the task manager.

GeekTip #1: Notifications are not sent to your app when the user clicks the Stop button by the system. It’s useful to look for the REASON USER REQUESTED reason in the ApplicationExitInfo API when your app restarts.

The following things happen when a user clicks the Stop button next to your program in the FGS Task Manager:

  • The playback of any media is halted.
  • Your app is deleted from the system’s memory. So, instead of simply the foreground service running, your entire app terminates.
  • The foreground service’s notification is no longer present.
  • Alarms are maintained.
  • Your app is not taken out of the history.
  • Jobs on the schedule are maintained.
  • The system deletes the activity back stack for your app.

Exemptions from any and all appearance in the FGS Task Manager:

The following applications can operate a foreground service while being completely invisible in the FGS Task Manager:

  1. System-level programs (Like Android Safety Net, and Play Protect Service API).
  2. Safety applications, specifically those with the ROLE EMERGENCY role on demo-mode devices
  3. Other apps which the Android Subsystem may think are important for your device to function properly
  4. The system apps are installed on your device by your OEM.

GeekTip #2: Notably, a few pre-installed applications that the maker has customized can also be exempt, this is done if you are running a ROM which is not vanilla.

Exemptions from user stoppable:

The FGS Task Manager displays the following app types when they run a foreground service, but there is no Stop button next to the program’s name for the user to click on:

  • Applications for device owners.
  • Applications with the role of a dialer.

You may see exceptions having their way all in Android’s various operations, and many of them are in place to actually protect your device from vital damage which it can take thinking that something is useful as a service. Exemptions apply to each app, not each process. All other processes in an app are exempt if the system exempts one process in that app.

List the sorts of foreground services:

Declare the location foreground service type as an attribute of your service> component if your app is designed for Android 10 (API level 29) or higher and accesses location data in a foreground service. You can also declare the camera foreground service so your app can not be killed by the Foreground Task manager while you are shooting an important memory, this is a feature that gets its code back in Android 11 but is implemented in this android iteration.

About the Android Manifest:

Each of the service types that you define in the app manifest is by default accessible when you call the startForeground() at runtime. As seen in the code snippets in the following sections, you can decide to restrict access to a portion of the declared service types.

Dispensations from the limitations:

In some circumstances, a foreground service can access location, camera, and microphone data when the app is operating in the foreground even if it was initiated while the app was running in the background (“while in use”). If an app with the ACCESS BACKGROUND LOCATION permission launches a service that defines a foreground service type of location in these circumstances, the service will always have access to location data, even when the app is operating in the background.


That is it, hope you understood how the Foreground Service Manager works in Android 13, and that you apply it in your apps, also a key takeaway is to see that many apps are already in the list of exceptions from Google as the system thinks that they are important to the overall system processes somehow, but this may change in the future iterations.

My Personal Notes arrow_drop_up
Last Updated : 26 Jan, 2023
Like Article
Save Article
Similar Reads