Gọi PowerManager.newWakeLock() để tạo một khoá đánh thức.
Thao tác này sẽ tạo và định cấu hình một đối tượng PowerManager.WakeLock nhưng không thực sự giữ cho thiết bị hoạt động.
Khi bạn muốn giữ cho thiết bị ở trạng thái thức, hãy gọi phương thức acquire() của đối tượng khoá chế độ thức.
Ví dụ: nếu ứng dụng của bạn có một broadcast receiver dùng một dịch vụ để thực hiện một số thao tác, bạn có thể dùng mã này để thiết lập và nhận khoá đánh thức:
Khi tạo đối tượng khoá đánh thức, mã sẽ dùng tên của lớp làm một phần của thẻ khoá đánh thức. Bạn nên đưa tên gói, lớp hoặc phương thức vào thẻ khoá chế độ thức. Bằng cách đó, nếu xảy ra lỗi, bạn sẽ dễ dàng xác định được khoá đánh thức trong mã nguồn của mình. Để biết thêm thông tin, hãy xem phần Đặt tên cho khoá chế độ thức một cách phù hợp.
WakeLock.acquire(long) được truyền một giá trị thời gian chờ tính bằng mili giây. Hệ thống sẽ huỷ khoá chế độ thức sau khoảng thời gian này, nếu bạn chưa huỷ khoá.
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-09-11 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-09-11 UTC."],[],[],null,["You can set a *wake lock* to temporarily keep the device awake.\n| **Note:** Creating and holding wake locks can have a dramatic impact on the device's battery life. You shouldn't use wake locks if there are any suitable alternatives. For other options, see the [Choose the right API to keep the device awake](/develop/background-work/background-tasks/awake) documentation. If you do need to use a wake lock, make sure to hold it for as short a time as possible.\n\nDependencies\n\nYour app must have the [`WAKE_LOCK`](/reference/android/Manifest.permission#WAKE_LOCK) permission to set a wake lock.\nAdd the permission to your app's manifest: \n\n \u003cuses-permission android:name=\"android.permission.WAKE_LOCK\" /\u003e\n\nCreate and acquire a wake lock\n\nTo acquire a wake lock, do the following:\n\n1. Call [`PowerManager.newWakeLock()`](/reference/android/os/PowerManager#newWakeLock(int,%20java.lang.String)) to create a wake lock.\n This creates and configures a `PowerManager.WakeLock` object but does not\n actually keep the device awake.\n\n2. When you want to keep the device awake, call the wake lock object's\n [`acquire()`](/reference/android/os/PowerManager.WakeLock#acquire(long)) method.\n\nFor example, if your app includes a broadcast receiver that uses a service to do\nsome work, you can use this code to set and acquire a wake lock:\n\n\nKotlin \n\n```kotlin\nval wakeLock: PowerManager.WakeLock =\n (getSystemService(POWER_SERVICE) as PowerManager).run {\n newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, \"MyClassName::MyWakelockTag\").apply {\n acquire(WAKELOCK_TIMEOUT)\n }\n }https://github.com/android/snippets/blob/9efabddf7d28b059560ab8e3d3e584fabe481501/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt#L29-L34\n```\n\nJava \n\n```java\nPowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);\nPowerManager.WakeLock wakeLock =\n powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, \"MyClassName::MyWakelockTag\");\nwakeLock.acquire(WAKELOCK_TIMEOUT);https://github.com/android/snippets/blob/9efabddf7d28b059560ab8e3d3e584fabe481501/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java#L19-L22\n```\n\n\u003cbr /\u003e\n\nKey points about this code\n\n- When the code creates the wake lock object, it uses the class's name as part\n of the wake lock tag. We recommend including your package, class, or method\n name as part of the wake lock tag. That way, if an error occurs, it's easier\n to locate the wake lock in your source code. For more information, see [Name\n the wake lock properly](/develop/background-work/background-tasks/awake/wakelock/best-practices#name).\n\n- `WakeLock.acquire(long)` is passed a timeout value in milliseconds. The\n system releases the wake lock after this much time passes, if you have not\n [already released it](/develop/background-work/background-tasks/awake/wakelock/release).\n\nSee also\n\n- [Release a wake lock](/develop/background-work/background-tasks/awake/wakelock/release)\n- [Follow wake lock best practices](/develop/background-work/background-tasks/awake/wakelock/best-practices)"]]