Rufen Sie PowerManager.newWakeLock() auf, um eine Wake Lock zu erstellen.
Dadurch wird ein PowerManager.WakeLock-Objekt erstellt und konfiguriert, das Gerät wird aber nicht tatsächlich im aktiven Zustand gehalten.
Wenn Sie das Gerät aktiv halten möchten, rufen Sie die Methode acquire() des Wake Lock-Objekts auf.
Wenn Ihre App beispielsweise einen Broadcast-Receiver enthält, der einen Dienst verwendet, um bestimmte Aufgaben auszuführen, können Sie mit diesem Code ein Wake Lock festlegen und abrufen:
Wenn der Code das Wake-Lock-Objekt erstellt, wird der Name der Klasse als Teil des Wake-Lock-Tags verwendet. Wir empfehlen, den Paket-, Klassen- oder Methodennamen als Teil des Wake-Lock-Tags anzugeben. So lässt sich das Wake Lock im Quellcode leichter finden, wenn ein Fehler auftritt. Weitere Informationen finden Sie unter Wake Lock richtig benennen.
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-31 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-31 (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()) 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(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/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsKotlin.kt#L28-L33\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();https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/misc/src/main/java/com/example/snippets/backgroundwork/WakeLockSnippetsJava.java#L17-L20\n```\n\n\u003cbr /\u003e\n\nKey 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- [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)"]]