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.
Khi người dùng để thiết bị chạy Android ở trạng thái rảnh, thiết bị sẽ nhanh chóng chuyển sang trạng thái tạm ngưng để tránh tiêu hao pin. Tuy nhiên, có những lúc ứng dụng cần ngăn CPU chuyển sang trạng thái tạm ngưng. Trong một số trường hợp, ứng dụng có thể cần phải giữ màn hình bật trong khi hoạt động. Trong các trường hợp khác, ứng dụng không cần bật màn hình nhưng vẫn cần CPU hoạt động.
Phương pháp bạn sử dụng phụ thuộc vào nhu cầu của ứng dụng. Tuy nhiên, quy tắc chung là bạn nên sử dụng phương pháp nhẹ nhất có thể để giảm thiểu tác động của ứng dụng đối với tài nguyên hệ thống. Tài liệu này giúp bạn chọn đúng công nghệ Android cho trường hợp của mình.
Chọn công nghệ phù hợp
Lựa chọn tốt nhất để giữ cho thiết bị của bạn luôn thức phụ thuộc vào nhu cầu của ứng dụng. Phần này giúp bạn chọn phương pháp phù hợp.
Ứng dụng của bạn có cần giữ màn hình bật không?
Nếu Có, hãy xem phần Bật màn hình. Có thể có một API có mục đích đặc biệt thực hiện những gì bạn cần; ví dụ: nếu đang triển khai giao diện người dùng cuộc gọi điện thoại, bạn có thể sử dụng khung viễn thông Android để giữ màn hình bật khi cần. Nếu không có API có mục đích đặc biệt cho trường hợp của bạn, bạn có thể sử dụng API keepScreenOn.
Ứng dụng của bạn có đang chạy một dịch vụ trên nền trước và bạn cần giữ cho thiết bị ở trạng thái thức khi màn hình tắt trong khi dịch vụ đang chạy không?
Nếu Không, bạn không cần giữ thiết bị ở chế độ bật. Nếu người dùng đang tích cực tương tác với ứng dụng, thiết bị sẽ luôn ở trạng thái thức. Nếu người dùng không tương tác với ứng dụng của bạn và bạn không chạy dịch vụ trên nền trước, thì bạn nên để thiết bị chuyển sang chế độ tạm ngưng khi cần. Nếu bạn chỉ cần đảm bảo một số công việc được thực hiện trong khi người dùng không sử dụng ứng dụng, hãy xem tài liệu về tác vụ trong nền để tìm lựa chọn tốt nhất.
Nếu Có, trước tiên, hãy xác nhận rằng bạn thực sự cần sử dụng dịch vụ trên nền trước. Tuỳ thuộc vào trường hợp của bạn, có thể bạn có thể sử dụng một số API có mục đích đặc biệt để thực hiện nhu cầu của mình thay vì dịch vụ trên nền trước.
Bạn có thể tìm thấy thông tin về các dịch vụ này trong tài liệu về Dịch vụ trên nền trước. Ví dụ: nếu cần theo dõi vị trí của người dùng, bạn có thể sử dụng API khoanh vùng địa lý thay vì dịch vụ trên nền trước location.
Việc thiết bị tạm ngưng trong khi dịch vụ trên nền trước đang chạy và màn hình thiết bị tắt có gây bất lợi cho trải nghiệm người dùng không? (Ví dụ: nếu bạn đang sử dụng dịch vụ trên nền trước để cập nhật thông báo, thì người dùng sẽ không gặp phải trải nghiệm tệ nếu thiết bị bị tạm ngưng.)
Nếu Không, đừng sử dụng khoá chế độ thức. Hành động sẽ tự động tiếp tục sau khi người dùng tương tác với thiết bị, giúp thiết bị thoát khỏi trạng thái tạm ngưng.
Nếu Có, bạn có thể cần sử dụng khoá chế độ thức. Tuy nhiên, bạn vẫn nên kiểm tra xem bạn có đang sử dụng API hoặc thực hiện hành động thay mặt bạn khai báo khoá chế độ thức hay không, như đã thảo luận trong phần Các hành động giúp thiết bị luôn thức.
Các thao tác khiến thiết bị luôn bật
Nếu ứng dụng của bạn đang thực hiện bất kỳ thao tác nào sau đây, thì bạn không cần tự đặt khoá chế độ thức. Các thao tác và API sau đây đều giúp thiết bị luôn thức.
Nếu bạn đang phát âm thanh, hệ thống âm thanh sẽ thiết lập và quản lý khoá chế độ thức cho bạn; bạn không cần phải tự làm việc này.
Nếu bạn đang sử dụng các API hoặc thư viện lập lịch tác vụ như WorkManager, JobScheduler hoặc DownloadManager, thì hệ thống hoặc thư viện sẽ thu nạp khoá chế độ thức được phân bổ cho ứng dụng của bạn.
Một số cảm biến thiết bị là cảm biến đánh thức; bạn có thể sử dụng SensorManager để các cảm biến đó đánh thức thiết bị khi có dữ liệu để báo cáo. Để kiểm tra xem một cảm biến có phải là cảm biến đánh thức hay không, hãy gọi Sensor.isWakeUpSensor.
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-07-26 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-07-26 UTC."],[],[],null,["# Choose the right API to keep the device awake\n\nWhen the user leaves their Android-powered device idle, it quickly goes into the\nsuspend state to avoid draining the battery. However, there are times when an\napp needs to prevent the CPU from going to the suspend state. In some cases, the\napp may need to keep the screen on while it's working. In other cases, the app\ndoesn't need to keep the screen on but still needs the CPU to be active.\n\nThe approach you take depends on the needs of your app. However, a general rule\nis that you should use the most lightweight approach possible, to minimize your\napp's impact on system resources. This document helps you choose the correct\nAndroid technology for your situation.\n| **Note:** You may be familiar with **wake locks**. An app can set a wake lock to keep the device from suspending. However, using a wake lock can quickly drain the device battery. You should only use a wake lock if there's no other option that will do what you need. If you do use a wake lock, you should release it as soon as possible.\n\nChoose the right technology\n---------------------------\n\nThe best option for keeping your device awake depends on your app's needs. This\nsection helps you choose the right approach.\n\n- Does your app need to keep the screen on?\n - If **Yes** , see [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on). There may be a special-purpose API that does what you need; for example, if you're implementing a phone-call UI, you can use the [Android telecom\n framework](/reference/android/telecom/package-summary), which keeps the screen on when needed. If there's no special purpose API for your situation, you can use the `keepScreenOn` API.\n- Is your app running a foreground service, and you need to keep the device awake when screen is off while the service is running?\n - If **No** , you do not need to keep the device awake. If the user is actively interacting with the app, the device will stay awake. If the user is not interacting with your app and you are not running a foreground service, you should let the device go into suspend mode when necessary. If you just need to make sure some work gets done while the user is away from the app, see the [background tasks](/develop/background-work/background-tasks) documentation to find the best option.\n - If **Yes** , first confirm that you actually need to use a foreground service. Depending on your situation, there may be some special-purpose API you can use to accomplish your need instead of a foreground service. You can find information about these [in the Foreground Service\n documentation](/develop/background-work/services/fgs/service-types). For example, if you need to track the user's location, you might be able to use the [geofencing API](/develop/sensors-and-location/location/geofencing) instead of a `location` foreground service.\n- Would it be detrimental to the user experience if the device suspends while the foreground service is running and the device screen is off? (For example, if you're using a foreground service to update notifications, it wouldn't be a bad user experience if the device is suspended.)\n - If **No**, do not use a wakelock. The action resumes automatically once the user engages with their device, which takes it out of suspend.\n - If **Yes** , you might need to [use a wake lock](/develop/background-work/background-tasks/awake/wakelock). However, you should still check to see if you're already using an API or doing an action that declares a wake lock on your behalf, as discussed in [Actions that keep the device awake](#actions-keep).\n\nActions that keep the device awake\n----------------------------------\n\nIf your app is doing any of the following, you don't need to set a wake lock\nyourself. The following actions and APIs all keep the device awake for you.\n\n- If you're playing audio, the audio system sets and manages a wake lock for you; you don't need to do it yourself.\n- If you're using task scheduling APIs or libraries such as [WorkManager](/develop/background-work/background-tasks/persistent), [`JobScheduler`](/reference/android/app/job/JobScheduler), or [`DownloadManager`](/reference/android/app/DownloadManager), the system or library acquires a wake lock that is attributed to your app.\n- If you're using [Media3 ExoPlayer](/media/media3/exoplayer), you can use [`ExoPlayer.setWakeMode()`](/reference/androidx/media3/exoplayer/ExoPlayer#setWakeMode(int)) to have the player set a wake lock for you.\n- Certain device sensors are wake-up sensors; you can use [`SensorManager`](/reference/android/hardware/SensorManager) to have those sensors wake up the device when they have data to report. To check if a sensor is a wake-up sensor, call [`Sensor.isWakeUpSensor`](/reference/android/hardware/Sensor#isWakeUpSensor())).\n\nSee also\n--------\n\n- [Use wake locks](/develop/background-work/background-tasks/awake/wakelock)\n- [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on)"]]