Untuk mendapatkan kunci tetap aktif, lakukan hal berikut:
Panggil PowerManager.newWakeLock() untuk membuat penguncian layar saat aktif.
Tindakan ini akan membuat dan mengonfigurasi objek PowerManager.WakeLock, tetapi tidak
benar-benar membuat perangkat tetap aktif.
Saat Anda ingin perangkat tetap aktif, panggil metode
acquire() objek penguncian layar saat aktif.
Misalnya, jika aplikasi Anda menyertakan penerima siaran yang menggunakan layanan untuk melakukan
beberapa pekerjaan, Anda dapat menggunakan kode ini untuk menyetel dan mendapatkan fitur penguncian layar saat aktif:
Saat membuat objek kunci tetap aktif, kode menggunakan nama class sebagai bagian dari tag kunci tetap aktif. Sebaiknya sertakan nama paket, class, atau metode
sebagai bagian dari tag penguncian layar saat aktif. Dengan begitu, jika terjadi error, akan lebih mudah untuk
menemukan kunci tetap aktif dalam kode sumber Anda. Untuk mengetahui informasi selengkapnya, lihat
Beri nama penguncian layar saat aktif dengan benar.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-30 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)"]]