Interrompere un servizio in primo piano
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Se vuoi che un servizio in primo piano smetta di essere eseguito in primo piano, hai
due opzioni. Puoi interrompere il servizio o lasciarlo in esecuzione, ma rimuoverlo dal primo piano.
Puoi interrompere un servizio in primo piano
nello stesso modo in cui interrompi qualsiasi servizio. Il servizio può
chiamare il proprio metodo stopSelf()
oppure un altro componente può interromperlo
chiamando stopService()
. Se interrompi il servizio mentre è in esecuzione
in primo piano, la relativa notifica viene rimossa.
Per rimuovere un servizio dal primo piano, chiama
stopForeground(int)
dall'interno del servizio. Questo metodo accetta un valore booleano che indica se
rimuovere anche la notifica della barra di stato. Il servizio continua a essere eseguito, ma
non è più un servizio in primo piano.
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.
[null,null,["Ultimo aggiornamento 2025-08-27 UTC."],[],[],null,["If you want a foreground service to stop running in the foreground, you have\ntwo options. You can stop the service, or you can leave the service\nrunning but remove it from the foreground.\n\nYou can stop a foreground service\n[the same way you would stop any service](/develop/background-work/services#Stopping). The service can\ncall its own [`stopSelf()`](/reference/android/app/Service#stopSelf()) method, or another component can stop it\nby calling [`stopService()`](/reference/android/content/Context#stopService(android.content.Intent)). If you stop the service while it runs\nin the foreground, its notification is removed.\n\nTo remove a service from the foreground, call\n[`stopForeground(int)`](/reference/android/app/Service#stopForeground(int))\nfrom inside the service. This method takes a boolean, which indicates whether to\nremove the status bar notification as well. The service continues to run, but\nit is no longer a foreground service."]]