自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
前景和背景使用者系統處理
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 行動裝置上,支援多位使用者可讓使用者在背景 (當其他使用者處於活動狀態時) 和前景 (又稱為「目前使用者」) 執行。為了在適當情況下節省資源,系統會管理使用者的關機作業。一律需要一個前景使用者。
自 Android 10 起,Android Automotive 的預設設定為「只允許」一次最多執行三個使用者 (config_multiuserMaxRunningUsers
)。因此,除了無頭系統使用者 (User 0) 之外,只能設定一個前景使用者和一個背景使用者。
- 在一般情況下,目前使用者會在前景執行,而無頭系統使用者 (使用者 0) 會在背景執行。當使用者移至背景時,系統會停止使用者,但不會鎖定使用者。達到使用者人數上限時,系統會停止並鎖定最近最少使用的背景使用者 (
config_multiuserDelayUserDataLocking
)。
- 在車庫模式期間,系統會重新啟動已停止且已解鎖的背景使用者。
訪客使用者是暫時性的,只能在前景執行。當使用者切換至訪客模式時,系統會停止訪客使用者,且無法在背景執行。
背景使用者程序
當使用者從前景切換至背景 (反之亦然) 時,該使用者的所有活動和前景服務都會終止。這會停止所有服務與這些服務的繫結。不過,仍需進行一些清理工作。只要第一方和原始設備製造商 (OEM) 系統應用程式的持續性服務持續執行,且使用者 (現在為背景) 未停止,就會繼續執行。
持續性服務的問題更嚴重,因為這些服務包含在 Android 記憶體不足 (OOM) 管理系統中,屬於高優先順序。即使前景使用者的應用程式需要更多記憶體,這些持續的背景程序也不會終止。因此,從前景使用者的角度來看,持續性服務會永久保留一定量的記憶體,只有在使用者重新啟動車輛,且所有背景使用者都已停止時,系統才會釋出記憶體。
使用者狀態
使用者會處於停止狀態 (STATE_SHUTDOWN
),直到使用者啟動為止。如果已設定使用者憑證 (例如 PIN 碼),Android 使用者會執行,但會保持鎖定狀態 (STATE_RUNNING_LOCKED
),直到使用者解鎖螢幕為止。當使用者解鎖裝置時,系統會解密其憑證加密儲存空間,並提供該使用者的資料目錄。對於一般使用者切換作業,系統不會停止背景使用者,而是會繼續執行並解鎖 (STATE_RUNNING_UNLOCKED
)。
使用者可用的車庫模式、JobScheduler 和應用程式更新
汽車應用程式更新資料的建議做法是使用 JobScheduler
,透過車庫模式安排工作,以便在裝置處於閒置狀態時執行 (例如從 Google Play 下載應用程式更新)。應用程式使用 JobScheduler
和 JobSchedulerService
註冊工作後,系統會在可行時執行工作。
CarService 會傳送訊號至 JobSchedulerService
,藉此觸發在汽車裝置處於停車模式時閒置的設定工作。JobSchedulerService
必須處於 STATE_RUNNING_UNLOCKED
狀態,才能為背景使用者執行作業。排入 JobSchedulerService
的工作會保留,並在電源週期中持續存在。
如果使用者在電源週期後從未解鎖,JobScheduler
就無法為該使用者執行工作。不過,如果使用者解鎖,且仍處於 STATE_RUNNING_UNLOCKED
中,則可執行使用者的工作。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Foreground and background user system handling\n\nIn mobile Android, support for multiple users enables users to run in the\nbackground (when another user is active) and in the foreground (also known as\nthe *current user* ). To conserve resources when appropriate, the system manages\nthe shutting down of users. *One foreground user is always required*.\n\nStarting with Android 10, Android Automotive has a default\nconfiguration of allowing a maximum of three users *only* to run at a time\n(`config_multiuserMaxRunningUsers`). Therefore, in addition to the headless\nsystem user (User 0), only one foreground user and one background user can be configured.\n\n- In typical circumstances, the current user runs in the foreground and the headless system user (User 0) runs in the background. When a user is moved to the background, the user is stopped but not locked. When the maximum number of users is met, the least recently used background user is stopped and locked (`config_multiuserDelayUserDataLocking`).\n- Stopped and unlocked background users are restarted during [Garage Mode](/docs/automotive/power/garage_mode).\n\nGuest users are ephemeral and can run only in the foreground. When a person\nswitches out of guest, the guest user is stopped and can't run in the background.\n\nBackground user processes\n-------------------------\n\nWhen a user is switched from foreground to background (and vice versa), all activities\nand foreground services for that user are terminated. This stops\nall services bound from those services. However, some clean up remains.\n[Persistent\nservices](https://developer.android.com/guide/topics/manifest/application-element.html#persistent) from first-party and OEM system apps continue to run for as long as\nthe (now background) user isn't stopped.\n\nPersistent services are more problematic, as these\nservices are contained in a high-priority bucket in Android's Out of Memory (OOM)\nmanagement system. Even if apps for the foreground user require more memory, those persistent\nbackground processes aren't terminated. As a result, from the foreground user's\npoint of view, the persistent services permanently carve out some amount of memory and\nthat memory is returned only when a person reboots the car and any background\nusers are stopped.\n\nUser state\n----------\n\nA user is in a stopped state (`STATE_SHUTDOWN`) until the user\nis started. If a user credential (such as a PIN) is set, the Android user runs but\nremains locked (`STATE_RUNNING_LOCKED`) until a person unlocks the lockscreen\nfor that user. When the user is unlocked, their [credential encrypted](/docs/security/features/encryption/file-based) storage is decrypted\nand the data directories for that user become available. For typical user switching,\nthe background user isn't stopped and remains running and unlocked\n(`STATE_RUNNING_UNLOCKED`), once unlocked.\n\nGarage Mode, JobScheduler, and app updates for users\n----------------------------------------------------\n\nThe recommended technique for Automotive apps to update data is to use [`JobScheduler`](https://developer.android.com/reference/android/app/job/JobScheduler)\nto schedule jobs to run when a device is in the idle state, through [Garage Mode](/docs/automotive/power/garage_mode) (for example, downloading\napp updates from Google Play). After apps register jobs with `JobScheduler` and\n[`JobSchedulerService`](https://android.googlesource.com/platform/frameworks/base/+/3426b72/services/core/java/com/android/server/job/JobSchedulerService.java),\nthe jobs are run when possible.\n\nCarService sends a signal to `JobSchedulerService` to trigger jobs set to run\nwhen the Automotive device is [idle](https://developer.android.com/reference/android/app/job/JobInfo.Builder#setRequiresDeviceIdle(boolean))\nthrough Garage Mode. For `JobSchedulerService` to run jobs for a background user, that\nuser must be in the state `STATE_RUNNING_UNLOCKED`. Jobs queued\ninto `JobSchedulerService` are persisted and survive across power cycles.\n\n`JobScheduler` can't run jobs for a specific user if the user was never\nunlocked after a power cycle. However, when the user is unlocked, and if the user remains in\n`STATE_RUNNING_UNLOCKED`, jobs for the user can be run."]]