Modifiche ai servizi in primo piano
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
La documentazione del servizio in primo piano descrive il comportamento attuale dei servizi in primo piano di Android. La documentazione fornisce indicazioni sulle best
practice per la maggior parte delle app, indipendentemente dal fatto che abbiano come target la versione più recente
di Android.
Questa pagina descrive alcune delle modifiche importanti più recenti ai servizi in primo piano e le implicazioni per le app che non hanno come target la versione più recente della piattaforma Android. In molti casi, le best practice che
erano facoltative per le app che hanno come target livelli API inferiori diventano obbligatorie per le app
che hanno come target livelli API superiori.
Android 16 (livello API 36)
Le seguenti modifiche si applicano alle app che vengono eseguite su Android 16 o versioni successive,
indipendentemente dal livello API target:
I job in background avviati da un servizio in primo piano ora devono rispettare le rispettive quote di runtime. Sono inclusi i job pianificati direttamente con
JobScheduler
, nonché i job creati da altre librerie come
WorkManager o DownloadManager
.
Per trasferire i dati in risposta a un'azione dell'utente, valuta la possibilità di utilizzare un
job di trasferimento di dati avviato dall'utente. Questi job sono esenti dalle quote di job ordinarie.
Android 15 (livello API 35)
I seguenti requisiti si applicano alle app che hanno come target il livello API 35 o versioni successive:
Android 14 (livello API 34)
I seguenti requisiti si applicano alle app che hanno come target il livello API 34 o versioni successive:
- Devi dichiarare tutti i servizi in primo piano con i relativi tipi di servizio.
- Le app devono richiedere il tipo di autorizzazione appropriato per il tipo di lavoro che il
servizio in primo piano svolgerà. Ogni tipo di servizio in primo piano
ha un tipo di autorizzazione corrispondente. Ad esempio, se un'app avvia un
servizio in primo piano che utilizza la fotocamera, devi richiedere le autorizzazioni
FOREGROUND_SERVICE
e
FOREGROUND_SERVICE_CAMERA
. Se un'app ha come target il livello API 34 o versioni successive e non richiede l'autorizzazione specifica appropriata, il sistema genera un errore SecurityException
.
Android 12 (livello API 31)
I seguenti requisiti si applicano alle app che hanno come target il livello API 31 o versioni successive:
Android 11 (livello API 30)
I seguenti requisiti si applicano alle app che hanno come target il livello API 30 o versioni successive:
Android 10 (livello API 29)
I seguenti requisiti si applicano alle app che hanno come target il livello API 29 o versioni successive:
Android 9 (livello API 28)
Android 9 introduce l'autorizzazione
FOREGROUND_SERVICE
. Le app in esecuzione su
Android 9 che utilizzano servizi in primo piano devono disporre di questa autorizzazione.
Se un'app che ha come target il livello API 28 o versioni successive tenta
di creare un servizio in primo piano senza richiedere l'autorizzazione FOREGROUND_SERVICE
, il sistema genera un errore SecurityException
.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-08-27 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-27 UTC."],[],[],null,["The foreground service documentation describes the current behavior of\nAndroid foreground services. The documentation gives guidance on best\npractices for most apps, whether or not they target the most recent version\nof Android.\n\nThis page describes some of the most recent important changes to foreground\nservices, and the implications for apps that aren't targeting the most\nrecent version of the Android platform. In many cases, best practices that\nwere optional for apps targeting lower API levels become mandatory for apps\nthat target higher API levels.\n\nAndroid 16 (API level 36)\n\nThe following changes apply to apps that run on Android 16 or higher,\nregardless of what API level they target:\n\n- Background jobs started from a foreground service now must adhere to their\n respective runtime quotas. This includes jobs scheduled directly with\n [`JobScheduler`](/reference/android/app/job/JobScheduler), as well as jobs created by other libraries like\n [WorkManager](/develop/background-work/background-tasks/persistent) or [`DownloadManager`](/reference/android/app/DownloadManager).\n\n To transfer data in response to a user action, consider using a\n [user-initiated data transfer job](/develop/background-work/background-tasks/uidt). These jobs are exempt from the\n ordinary job quotas.\n\nAndroid 15 (API level 35)\n\nThe following requirements apply to apps that target API\nlevel 35 or higher:\n\n- There are new restrictions on how long a `dataSync` foreground service can run. These restrictions are described in [Foreground service timeout\n behavior](/develop/background-work/services/fgs/timeout). Similar restrictions apply to the (new in Android 15) `mediaProcessing` foreground service type.\n- [`BOOT_COMPLETED` foreground services are no longer allowed to launch certain\n foreground services](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n- Apps that hold the `SYSTEM_ALERT_WINDOW` permission are only allowed to launch foreground services from the background if they currently have a visible overlay window (or if they meet one of the other [exemptions from background\n start restrictions](/develop/background-work/services/fgs/restrictions-bg-start#background-start-restriction-exemptions)). Previously, the exemption for those apps was broader.\n\nAndroid 14 (API level 34)\n\nThe following requirements apply to apps that target API\nlevel 34 or higher:\n\n- You must [declare all foreground services](/develop/background-work/services/fgs/declare) with their service types.\n- Apps must request the appropriate permission type for the kind of work the foreground service will be doing. Each [foreground service type](/develop/background-work/services/fgs/service-types) has a corresponding permission type. For example, if an app launches a foreground service that uses the camera, you must request both the [`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) and [`FOREGROUND_SERVICE_CAMERA`](/reference/android/Manifest.permission#FOREGROUND_SERVICE_CAMERA) permissions. If an app targets API level 34 or higher and doesn't request the appropriate specific permission, the system throws a `SecurityException`.\n\nAndroid 12 (API level 31)\n\nThe following requirements apply to apps that target API\nlevel 31 or higher:\n\n- Apps are not allowed to launch foreground services while the app is in the background, with a few specific exceptions. For more information, and information about the exceptions to this rule, see [Restrictions on starting\n a foreground service from the background](/develop/background-work/services/fgs/restrictions-bg-start).\n\nAndroid 11 (API level 30)\n\nThe following requirements apply to apps that target API\nlevel 30 or higher:\n\n- If an app's foreground services use the camera or microphone, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`camera`](/develop/background-work/services/fgs/service-types#camera) or [`microphone`](/develop/background-work/services/fgs/service-types#microphone) service type, respectively.\n\nAndroid 10 (API level 29)\n\nThe following requirements apply to apps that target API\nlevel 29 or higher:\n\n- If an app's foreground services use location information, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`location`](/develop/background-work/services/fgs/service-types#location) service type.\n\nAndroid 9 (API level 28)\n\nAndroid 9 introduces the\n[`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) permission. Apps running on\nAndroid 9 that use foreground services must have that permission.\n\nIf an app that targets API level 28 or higher attempts\nto create a foreground service without requesting the `FOREGROUND_SERVICE`\npermission, the system throws a [`SecurityException`](/reference/java/lang/SecurityException)."]]