停止前景服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要停止前景服務在前台執行,有兩種做法。您可以停止服務,也可以讓服務繼續執行,但從前景移除。
您可以停止任何服務,前景服務也不例外。服務可以呼叫自己的 stopSelf()
方法,也可以由其他元件呼叫 stopService()
停止服務。如果停止在前台執行的服務,系統會移除相關通知。
如要從前景移除服務,請從服務內部呼叫 stopForeground(int)
。這個方法會採用布林值,指出是否也要移除狀態列通知。服務會繼續執行,但不再是前景服務。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["# Stop a foreground service\n\nIf 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."]]