Tổng quan về 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.
Dịch vụ trên nền trước cho phép bạn thực hiện không đồng bộ các thao tác mà người dùng có thể nhận thấy. Dịch vụ trên nền trước sẽ hiển thị một thông báo trên thanh trạng thái để giúp người dùng biết rằng ứng dụng của bạn đang thực hiện một nhiệm vụ ở nền trước và đang sử dụng tài nguyên hệ thống.
Sau đây là ví dụ về các ứng dụng sử dụng dịch vụ trên nền trước:
- Một ứng dụng trình phát nhạc có chức năng phát nhạc trong dịch vụ trên nền trước. Thông báo có thể hiển thị bài hát đang phát.
- Một ứng dụng thể dục ghi lại quãng đường chạy của một người dùng trong dịch vụ trên nền trước sau khi được người dùng đó cho phép. Thông báo có thể cho biết khoảng cách mà người dùng đã di chuyển trong phiên tập thể dục hiện tại.
Chỉ sử dụng dịch vụ trên nền trước khi ứng dụng của bạn cần thực hiện một nhiệm vụ mà người dùng có thể nhận ra, ngay cả khi họ không tương tác trực tiếp với ứng dụng. Nếu hành động có tầm quan trọng thấp đến mức bạn muốn sử dụng thông báo có mức độ ưu tiên tối thiểu, thì có thể bạn nên sử dụng một lựa chọn khác cho hoạt động ở chế độ nền.
Hướng dẫn này giải thích các khía cạnh sau:
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-21 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-21 UTC."],[],[],null,["Foreground 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)"]]