[null,null,["最后更新时间 (UTC):2025-08-25。"],[],[],null,["# Set a wake lock\n\nYou 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------------\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------------------------------\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()) 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\n### Kotlin\n\n```kotlin\nval wakeLock: PowerManager.WakeLock =\n (getSystemService(Context.POWER_SERVICE) as PowerManager).run {\n newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, \"MyClassName::MyWakelockTag\").apply {\n acquire()\n }\n }https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt#L28-L33\n```\n\n### Java\n\n```java\nPowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);\nPowerManager.WakeLock wakeLock =\n powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, \"MyClassName::MyWakelockTag\");\nwakeLock.acquire();https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java#L17-L20\n```\n\n\u003cbr /\u003e\n\n### Key points about this code\n\nWhen the code creates the wake lock object, it uses the class's name as part\nof the wake lock tag. We recommend including your package, class, or method name\nas part of the wake lock tag. That way, if an error occurs, it's easier to\nlocate the wake lock in your source code. For more information, see\n[Name the wake lock properly](/develop/background-work/background-tasks/awake/wakelock/best-practices#name).\n\nSee also\n--------\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)"]]