Các thay đổi đối với dịch vụ trên nền trước
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài liệu về dịch vụ trên nền trước mô tả hành vi hiện tại của các dịch vụ trên nền trước của Android. Tài liệu này đưa ra hướng dẫn về các phương pháp hay nhất cho hầu hết các ứng dụng, bất kể ứng dụng đó có nhắm đến phiên bản Android mới nhất hay không.
Trang này mô tả một số thay đổi quan trọng gần đây nhất đối với các dịch vụ trên nền trước và những tác động đối với các ứng dụng không nhắm đến phiên bản mới nhất của nền tảng Android. Trong nhiều trường hợp, các phương pháp hay nhất không bắt buộc đối với ứng dụng nhắm đến API cấp thấp hơn sẽ trở thành bắt buộc đối với ứng dụng nhắm đến API cấp cao hơn.
Android 16 (API cấp 36)
Những thay đổi sau đây áp dụng cho các ứng dụng chạy trên Android 16 trở lên, bất kể ứng dụng đó nhắm đến cấp độ API nào:
Các tác vụ trong nền được bắt đầu từ một dịch vụ trên nền trước hiện phải tuân thủ hạn mức thời gian chạy tương ứng. Điều này bao gồm cả những công việc được lên lịch trực tiếp bằng JobScheduler
, cũng như những công việc do các thư viện khác tạo ra, chẳng hạn như WorkManager hoặc DownloadManager
.
Để chuyển dữ liệu theo hành động của người dùng, hãy cân nhắc sử dụng công việc chuyển dữ liệu do người dùng khởi tạo. Những công việc này được miễn hạn ngạch việc làm thông thường.
Android 15 (API cấp 35)
Các yêu cầu sau đây áp dụng cho những ứng dụng nhắm đến API cấp 35 trở lên:
Android 14 (API cấp 34)
Các yêu cầu sau đây áp dụng cho những ứng dụng nhắm đến API cấp 34 trở lên:
Android 12 (API cấp 31)
Các yêu cầu sau đây áp dụng cho những ứng dụng nhắm đến API cấp 31 trở lên:
Android 11 (API cấp 30)
Các yêu cầu sau đây áp dụng cho những ứng dụng nhắm đến API cấp 30 trở lên:
- Nếu các dịch vụ trên nền trước của một ứng dụng sử dụng camera hoặc micrô, thì ứng dụng đó phải khai báo dịch vụ bằng loại dịch vụ
camera
hoặc microphone
tương ứng.
Android 10 (API cấp 29)
Các yêu cầu sau đây áp dụng cho những ứng dụng nhắm đến API cấp 29 trở lên:
- Nếu các dịch vụ trên nền trước của một ứng dụng sử dụng thông tin vị trí, thì ứng dụng đó phải khai báo dịch vụ bằng loại dịch vụ
location
.
Android 9 (API cấp 28)
Android 9 giới thiệu quyền FOREGROUND_SERVICE
. Những ứng dụng chạy trên Android 9 và sử dụng dịch vụ trên nền trước phải có quyền đó.
Nếu một ứng dụng nhắm đến API cấp 28 trở lên cố gắng tạo một dịch vụ trên nền trước mà không yêu cầu quyền FOREGROUND_SERVICE
, thì hệ thống sẽ gửi một SecurityException
.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[],[],null,["The foreground service documentation describes the current behavior of\nAndroid foreground services. The documentation gives guidance on best\npractices for most apps, whether or not they target the most recent version\nof Android.\n\nThis page describes some of the most recent important changes to foreground\nservices, and the implications for apps that aren't targeting the most\nrecent version of the Android platform. In many cases, best practices that\nwere optional for apps targeting lower API levels become mandatory for apps\nthat target higher API levels.\n\nAndroid 16 (API level 36)\n\nThe following changes apply to apps that run on Android 16 or higher,\nregardless of what API level they target:\n\n- Background jobs started from a foreground service now must adhere to their\n respective runtime quotas. This includes jobs scheduled directly with\n [`JobScheduler`](/reference/android/app/job/JobScheduler), as well as jobs created by other libraries like\n [WorkManager](/develop/background-work/background-tasks/persistent) or [`DownloadManager`](/reference/android/app/DownloadManager).\n\n To transfer data in response to a user action, consider using a\n [user-initiated data transfer job](/develop/background-work/background-tasks/uidt). These jobs are exempt from the\n ordinary job quotas.\n\nAndroid 15 (API level 35)\n\nThe following requirements apply to apps that target API\nlevel 35 or higher:\n\n- There are new restrictions on how long a `dataSync` foreground service can run. These restrictions are described in [Foreground service timeout\n behavior](/develop/background-work/services/fgs/timeout). Similar restrictions apply to the (new in Android 15) `mediaProcessing` foreground service type.\n- [`BOOT_COMPLETED` foreground services are no longer allowed to launch certain\n foreground services](/about/versions/15/behavior-changes-15#fgs-boot-completed).\n- Apps that hold the `SYSTEM_ALERT_WINDOW` permission are only allowed to launch foreground services from the background if they currently have a visible overlay window (or if they meet one of the other [exemptions from background\n start restrictions](/develop/background-work/services/fgs/restrictions-bg-start#background-start-restriction-exemptions)). Previously, the exemption for those apps was broader.\n\nAndroid 14 (API level 34)\n\nThe following requirements apply to apps that target API\nlevel 34 or higher:\n\n- You must [declare all foreground services](/develop/background-work/services/fgs/declare) with their service types.\n- Apps must request the appropriate permission type for the kind of work the foreground service will be doing. Each [foreground service type](/develop/background-work/services/fgs/service-types) has a corresponding permission type. For example, if an app launches a foreground service that uses the camera, you must request both the [`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) and [`FOREGROUND_SERVICE_CAMERA`](/reference/android/Manifest.permission#FOREGROUND_SERVICE_CAMERA) permissions. If an app targets API level 34 or higher and doesn't request the appropriate specific permission, the system throws a `SecurityException`.\n\nAndroid 12 (API level 31)\n\nThe following requirements apply to apps that target API\nlevel 31 or higher:\n\n- Apps are not allowed to launch foreground services while the app is in the background, with a few specific exceptions. For more information, and information about the exceptions to this rule, see [Restrictions on starting\n a foreground service from the background](/develop/background-work/services/fgs/restrictions-bg-start).\n\nAndroid 11 (API level 30)\n\nThe following requirements apply to apps that target API\nlevel 30 or higher:\n\n- If an app's foreground services use the camera or microphone, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`camera`](/develop/background-work/services/fgs/service-types#camera) or [`microphone`](/develop/background-work/services/fgs/service-types#microphone) service type, respectively.\n\nAndroid 10 (API level 29)\n\nThe following requirements apply to apps that target API\nlevel 29 or higher:\n\n- If an app's foreground services use location information, the app must [declare the service](/develop/background-work/services/fgs/declare) with the [`location`](/develop/background-work/services/fgs/service-types#location) service type.\n\nAndroid 9 (API level 28)\n\nAndroid 9 introduces the\n[`FOREGROUND_SERVICE`](/reference/android/Manifest.permission#FOREGROUND_SERVICE) permission. Apps running on\nAndroid 9 that use foreground services must have that permission.\n\nIf an app that targets API level 28 or higher attempts\nto create a foreground service without requesting the `FOREGROUND_SERVICE`\npermission, the system throws a [`SecurityException`](/reference/java/lang/SecurityException)."]]