Android 15 的前景服務類型異動
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
We are making the following changes to foreground service types with Android
15.
- 要在下列資訊清單中宣告的前景服務類型
android:foregroundServiceType
mediaProcessing
- 在資訊清單中宣告的權限
FOREGROUND_SERVICE_MEDIA_PROCESSING
- 要傳遞至
startForeground()
的常數
FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING
- 執行階段必要條件
- 無
- 說明
這項服務可在媒體資產上執行耗時的作業,例如將媒體轉換為其他格式。系統會允許這項服務執行的時間有限,在一般情況下,這個時間限制是每 24 小時 6 小時。(這項限制適用於應用程式的所有 mediaProcessing
前景服務)。
在下列情況下,應用程式應手動停止媒體處理服務:
如果逾時,系統會呼叫服務的 Service.onTimeout(int, int)
方法。此時,服務有幾秒的時間可以呼叫 Service.stopSelf()
。如果服務未呼叫 Service.stopSelf()
,系統會發生 ANR,並顯示以下錯誤訊息:「<fgs_type> 的前景服務未在逾時期限內停止:<component_name>」。
注意:Service.onTimeout(int, int)
不適用於 Android 14 以下版本。在執行這些版本的裝置上,如果媒體處理服務達到逾時期限,系統會立即快取應用程式。因此,應用程式不應等待逾時通知。而是應盡快終止前景服務,或將其變更為背景服務。
相機
指定 Android 15 以上版本為目標版本的應用程式,不得透過 BOOT_COMPLETED
廣播接收器啟動相機前景服務。詳情請參閱「限制 BOOT_COMPLETED
廣播接收器啟動前景服務」。
同步處理資料
指定 Android 15 以上版本為目標版本的應用程式,不得透過 BOOT_COMPLETED
廣播接收器啟動資料同步前景服務。詳情請參閱「限制 BOOT_COMPLETED
廣播接收器啟動前景服務」。
指定 Android 15 以上版本為目標版本的應用程式,不得透過 BOOT_COMPLETED
廣播接收器啟動媒體播放前景服務。詳情請參閱「限制 BOOT_COMPLETED
廣播接收器啟動前景服務」。
指定 Android 15 以上版本為目標版本的應用程式,不得透過 BOOT_COMPLETED
廣播接收器啟動媒體投影前景服務。詳情請參閱「限制 BOOT_COMPLETED
廣播接收器啟動前景服務」。
麥克風
Apps that target Android 14 (API level 34) or higher are not allowed to launch a
microphone foreground service from a BOOT_COMPLETED
broadcast receiver.
For more information, see
Restrictions on BOOT_COMPLETED
broadcast receivers launching foreground
services.
語音通話
Apps that target Android 15 or higher are not allowed to launch a
phone call foreground service from a BOOT_COMPLETED
broadcast receiver.
For more information, see
Restrictions on BOOT_COMPLETED
broadcast receivers launching foreground
services.
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Changes to foreground service types for Android 15\n\nWe are making the following changes to foreground service types with Android\n15.\n\n### Media processing\n\nForeground service type to declare in manifest under\n`android:foregroundServiceType`\n: `mediaProcessing`\n\nPermission to declare in your manifest\n: `FOREGROUND_SERVICE_MEDIA_PROCESSING`\n\nConstant to pass to `startForeground()`\n: `FOREGROUND_SERVICE_TYPE_MEDIA_PROCESSING`\n\nRuntime prerequisites\n: None\n\nDescription\n\n: Service for performing time-consuming operations on media assets, like\n converting media to different formats. The system allows this service a limited\n time to run; under normal circumstances, this time limit would be 6 hours out of\n every 24. (This limit is shared by all of an app's `mediaProcessing` foreground\n services.)\n\n: Your app should manually stop the media processing service in the following\n scenario:\n\n - When the transcoding operation finishes or reaches a failure state, have the service call [`Service.stopForeground()`](/reference/android/app/Service#stopForeground(int)) and [`Service.stopSelf()`](/reference/android/app/Service#stopSelf()) to stop the service completely.\n\n: If the timeout period is reached, the system calls the service's\n [`Service.onTimeout(int, int)`](/reference/android/app/Service#onTimeout(int,%20int)) method. At this\n time, the service has a few\n seconds to call [`Service.stopSelf()`](/reference/android/app/Service#stopSelf()). If the service does not call\n `Service.stopSelf()`, an ANR will occur with this error message: \"A\n foreground service of *\\\u003cfgs_type\\\u003e* did not stop within its\n timeout: *\\\u003ccomponent_name\\\u003e*\".\n\n **Note** : `Service.onTimeout(int, int)` is not available on Android 14\n or lower. On devices running those versions, if a media processing\n service reaches the timeout period, the system immediately caches the app.\n For this reason, your app shouldn't wait to get a timeout notification.\n Instead, it should terminate the foreground service or change it to a\n background service as soon as appropriate.\n\n### Camera\n\nApps that target Android 15 or higher are not allowed to launch a\ncamera foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n\n### Data sync\n\nApps that target Android 15 or higher are not allowed to launch a\ndata sync foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n\n### Media playback\n\nApps that target Android 15 or higher are not allowed to launch a\nmedia playback foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n\n### Media projection\n\nApps that target Android 15 or higher are not allowed to launch a\nmedia projection foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n\n### Microphone\n\nApps that target Android 14 (API level 34) or higher are not allowed to launch a\nmicrophone foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n\n### Phone call\n\nApps that target Android 15 or higher are not allowed to launch a\nphone call foreground service from a `BOOT_COMPLETED` broadcast receiver.\nFor more information, see\n[Restrictions on `BOOT_COMPLETED` broadcast receivers launching foreground\nservices](/about/versions/15/behavior-changes-15#fgs-boot-completed)."]]