Chame PowerManager.newWakeLock() para criar um bloqueio de despertar.
Isso cria e configura um objeto PowerManager.WakeLock, mas não
mantém o dispositivo ativo.
Quando quiser manter o dispositivo ativo, chame o método
acquire() do objeto wake lock.
Por exemplo, se o app incluir um broadcast receiver que usa um serviço para fazer
algum trabalho, você poderá usar este código para definir e adquirir um wake lock:
Quando o código cria o objeto de bloqueio de despertar, ele usa o nome da classe como parte
da tag de bloqueio de despertar. Recomendamos incluir o nome do pacote, da classe ou do método
como parte da tag de wake lock. Assim, se um erro ocorrer, será mais fácil
localizar o bloqueio de despertar no código-fonte. Para mais informações, consulte
Nomear o wake lock corretamente.
O conteúdo e os exemplos de código nesta página estão sujeitos às licenças descritas na Licença de conteúdo. Java e OpenJDK são marcas registradas da Oracle e/ou suas afiliadas.
Última atualização 2025-08-30 UTC.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Não contém as informações de que eu preciso","missingTheInformationINeed","thumb-down"],["Muito complicado / etapas demais","tooComplicatedTooManySteps","thumb-down"],["Desatualizado","outOfDate","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Problema com as amostras / o código","samplesCodeIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-30 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)"]]