前景服務總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
前景服務可讓您以非同步方式執行使用者可察覺的作業。前景服務會顯示狀態列通知,讓使用者瞭解應用程式正在前景執行工作及消耗系統資源。
使用前景服務的應用程式範例如下:
- 音樂播放器應用程式在前景服務中播放音樂,並透過通知顯示目前播放的歌曲。
- 健身應用程式先取得使用者授予的權限,然後在前景服務中記錄跑步情況。通知可能會顯示使用者在目前健身活動中移動的距離。
只有在應用程式需要執行使用者可察覺的工作時,才能使用前景服務,就算使用者並未直接與應用程式互動也一樣。如果動作的重要性不高,您想使用最低優先權的通知,可能需要使用其他背景工作選項。
本指南說明下列領域:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["# Foreground services overview\n\nForeground services let you asynchronously perform operations that are\nnoticeable to the user. Foreground services show a [status bar\nnotification](/develop/ui/views/notifications), to make users aware that your\napp is performing a task in the foreground and is consuming system resources.\n\nExamples of apps that use foreground services include the following:\n\n- A music player app that plays music in a foreground service. The notification might show the current song being played.\n- A fitness app that records a user's run in a foreground service, after receiving permission from the user. The notification might show the distance that the user has traveled during the current fitness session.\n\nOnly use a foreground service when your app needs to perform a task\nthat is noticeable by the user, even when they're not directly interacting with\nthe app. If the action is of low enough importance that you want to use a\nminimum-priority notification, you probably want to use a different\n[background work option](/develop/background-work/background-tasks).\n\nThis guide explains the following areas:\n\n- [Declare foreground services and request\n permissions](/develop/background-work/services/fgs/declare)\n- [Launch a foreground service](/develop/background-work/services/fgs/launch)\n- [Stop a foreground service](/develop/background-work/services/fgs/stop-fgs)\n- [Handle when a user stops an app that has a foreground\n service](/develop/background-work/services/fgs/handle-user-stopping)\n- [Restrictions on starting a foreground service from the background](/develop/background-work/services/fgs/restrictions-bg-start)\n- [Foreground service types](/develop/background-work/services/fgs/service-types)\n- [Foreground service timeout behavior](/develop/background-work/services/fgs/timeout)\n- [Foreground service troubleshooting](/develop/background-work/services/fgs/troubleshooting)\n- [Changes to foreground services](/develop/background-work/services/fgs/changes)"]]