Introduction to Foreground Services
Foreground services play a vital role in Android app development, especially for tasks requiring continuous system resource utilization and user awareness. These services run in the foreground, displaying a status bar notification to inform users that the app is performing a task and consuming system resources.
Declaring Foreground Service Types in Android 14
Starting with Android 14, apps must declare the appropriate foreground service types in the manifest file. This ensures compliance with new policies and regulations introduced by Google. The declaration involves specifying the type of foreground service used by the app, such as dataSync
, health
, or shortService
, along with the necessary permissions.
Example Declarations
- Data Sync Service: Declare the
FOREGROUND_SERVICE_DATA_SYNC
permission. - Health Service: Declare the
FOREGROUND_SERVICE_HEALTH
permission and meet specific runtime prerequisites, such as requesting and being granted theHIGH_SAMPLING_RATE_SENSORS
permission or at least one of theBODY_SENSORS
runtime permissions.
User-Initiated Data Transfer Jobs
Android 14 introduces user-initiated data transfer jobs as an alternative to the traditional dataSync
foreground service. These jobs simplify over-the-network data transfers and automatically manage wakelocks. They are particularly useful for transferring data locally or over a network in response to an explicit user request.
Full-Screen Intent Requirements
Significant changes in Android 14 affect the handling of full-screen intents. The USE_FULL_SCREEN_INTENT
permission, previously a normal permission, is now a special app access permission. Only apps with core functionality involving high-priority use cases, such as setting an alarm or receiving phone or video calls, will be automatically granted this permission. Other apps may use this permission only after user approval.
Declaring Core Functionality
Developers can declare their app as a core functionality app for full-screen intent on the App content page in the Play Console. This declaration will be available starting April 2024.
Handling Foreground Services in Code
To start a foreground service in Android, follow these steps:
- Create an Intent: Build an intent to start the service.
- Start the Service: Use the
startForegroundService
method. - Request Foreground: Inside the service, usually in the
onStartCommand
method, callServiceCompat.startForeground
to request that your service run in the foreground. This method takes the service, a positive integer that uniquely identifies the notification in the status bar, and theNotification
object itself as parameters. Additionally, specify the foreground service types identifying the work done by the service.
Example Use Case
For a fitness app tracking a user’s exercise and playing media, declare both location
and mediaPlayback
in the manifest. If a user starts a run and wants only location tracking, call startForeground
and pass just the ACCESS_FINE_LOCATION
permission. If the user starts playing audio, call startForeground
again and pass the bitwise combination of all the foreground service types (in this case, ACCESS_FINE_LOCATION | FOREGROUND_SERVICE_MEDIA_PLAYBACK
).
User-Initiated Stopping of Apps Running Foreground Services
Starting in Android 13 (API level 33), users can stop an app with an ongoing foreground service from the notification drawer. This feature, called the Task Manager, shows a list of apps currently running a foreground service. Users can press the Stop button next to each app to remove it from memory, stop media playback, and remove the associated notification. The app remains in history, and scheduled jobs execute at their scheduled time.
Exemptions from Task Manager
The system provides several levels of exemptions for certain types of apps. These exemptions are per app, not per process.
Examples of Exemptions
- System-level apps: Can run a foreground service and not appear in the Task Manager.
- Safety apps: Apps with the
ROLE_EMERGENCY
role are exempt from appearing in the Task Manager. - Devices in demo mode: Do not appear in the Task Manager.
- Device owner apps: Appear in the Task Manager but do not have a Stop button.
- Profile owner apps: Similar to device owner apps, these also appear in the Task Manager but do not have a Stop button.
- Persistent apps: Appear in the Task Manager but do not have a Stop button.
- Apps with the ROLE_DIALER role: Appear in the Task Manager but do not have a Stop button.
New Foreground Service Types in Android 15
Android 15 introduces new changes to foreground services, including a new timeout behavior for dataSync
and mediaProcessing
foreground services. The system permits an app's dataSync
services to run for a total of 6 hours in a 24-hour period. If the service does not stop within this time frame, the system calls the running service's Service.onTimeout(int, int)
method. The service has a few seconds to call Service.stopSelf()
to avoid generating an internal exception.
Media Processing Service
Android 15 introduces a new foreground service type called mediaProcessing
. This service type is appropriate for operations like transcoding media files. The system permits an app's mediaProcessing
services to run for a total of 6 hours in a 24-hour period, after which the system calls the running service's Service.onTimeout(int, int)
method.
Restrictions on Starting Foreground Services
Android 15 also introduces restrictions on starting foreground services while an app holds the SYSTEM_ALERT_WINDOW
permission. This change aims to prevent apps from misusing system resources and notifications.
Final Thoughts
Android's handling of foreground services has evolved significantly with each new version. From declaring specific foreground service types and permissions to introducing new service types and restrictions, these changes aim to enhance user experience and ensure that apps use system resources responsibly. By understanding these requirements and implementing them correctly, developers can create more robust and compliant apps that meet the evolving standards of Android.