Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Saat pengguna membiarkan perangkat Android tidak ada aktivitas selama beberapa saat, perangkat akan segera beralih ke
status penangguhan untuk menghindari pengosongan baterai. Namun, ada kalanya
aplikasi perlu mencegah CPU beralih ke status penangguhan. Dalam beberapa kasus, aplikasi mungkin perlu menjaga layar tetap aktif saat aplikasi sedang berjalan. Dalam kasus lain, aplikasi
tidak perlu terus mengaktifkan layar, tetapi masih memerlukan CPU untuk aktif.
Pendekatan yang diambil bergantung pada kebutuhan aplikasi. Namun, aturan umum
adalah Anda harus menggunakan pendekatan yang paling ringan, untuk meminimalkan
dampak aplikasi terhadap resource sistem. Dokumen ini membantu Anda memilih teknologi Android
yang tepat untuk situasi Anda.
Memilih teknologi yang tepat
Opsi terbaik untuk membuat perangkat tetap aktif bergantung pada kebutuhan aplikasi Anda. Bagian
ini membantu Anda memilih pendekatan yang tepat.
Apakah aplikasi Anda perlu tetap menyalakan layar?
Jika Ya, lihat Membiarkan layar tetap menyala. Mungkin ada
API tujuan khusus yang melakukan hal yang Anda butuhkan; misalnya, jika Anda
menerapkan UI panggilan telepon, Anda dapat menggunakan framework telekomunikasi
Android, yang membuat layar tetap menyala saat diperlukan. Jika tidak ada
API tujuan khusus untuk situasi Anda, Anda dapat menggunakan keepScreenOn
API.
Apakah aplikasi Anda menjalankan layanan latar depan, dan Anda perlu membuat perangkat
tetap aktif saat layar nonaktif saat layanan berjalan?
Jika Tidak, Anda tidak perlu membuat perangkat tetap aktif. Jika pengguna
secara aktif berinteraksi dengan aplikasi, perangkat akan tetap aktif. Jika
pengguna tidak berinteraksi dengan aplikasi Anda dan Anda tidak menjalankan
layanan latar depan, Anda harus membiarkan perangkat beralih ke mode penangguhan jika
diperlukan. Jika Anda hanya perlu memastikan beberapa pekerjaan selesai saat
pengguna tidak berada di aplikasi, lihat dokumentasi tugas latar belakang
untuk menemukan opsi terbaik.
Jika Ya, konfirmasi terlebih dahulu bahwa Anda benar-benar perlu menggunakan layanan
latar depan. Bergantung pada situasi Anda, mungkin ada beberapa API tujuan khusus
yang dapat Anda gunakan untuk memenuhi kebutuhan Anda, bukan layanan latar depan.
Anda dapat menemukan informasi tentang hal ini dalam dokumentasi Layanan Latar
Depan. Misalnya, jika perlu melacak lokasi
pengguna, Anda mungkin dapat menggunakan API pembatasan wilayah, bukan
layanan latar depan location.
Apakah akan merugikan pengalaman pengguna jika perangkat ditangguhkan saat
layanan latar depan berjalan dan layar perangkat nonaktif? (Misalnya, jika Anda menggunakan layanan latar depan untuk memperbarui notifikasi, pengalaman pengguna
tidak akan buruk jika perangkat ditangguhkan.)
Jika Tidak, jangan gunakan wakelock. Tindakan akan dilanjutkan
secara otomatis setelah pengguna berinteraksi dengan perangkatnya, yang akan menghentikan
penangguhan.
Jika aplikasi Anda melakukan salah satu hal berikut, Anda tidak perlu menetapkan penguncian layar saat aktif
sendiri. Semua tindakan dan API berikut akan membuat perangkat tetap aktif untuk Anda.
Jika Anda memutar audio, sistem audio akan menetapkan dan mengelola kunci layar aktif untuk
Anda; Anda tidak perlu melakukannya sendiri.
Jika Anda menggunakan API atau library penjadwalan tugas seperti WorkManager,
JobScheduler, atau DownloadManager, sistem atau library akan memperoleh
kunci layar yang diatribusikan ke aplikasi Anda.
Sensor perangkat tertentu adalah sensor pengaktifan; Anda dapat menggunakan SensorManager
agar sensor tersebut mengaktifkan perangkat saat memiliki data untuk dilaporkan. Untuk
memeriksa apakah sensor adalah sensor pengaktifan, panggil Sensor.isWakeUpSensor.
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-07-26 UTC.
[null,null,["Terakhir diperbarui pada 2025-07-26 UTC."],[],[],null,["# Choose the right API to keep the device awake\n\nWhen the user leaves their Android-powered device idle, it quickly goes into the\nsuspend state to avoid draining the battery. However, there are times when an\napp needs to prevent the CPU from going to the suspend state. In some cases, the\napp may need to keep the screen on while it's working. In other cases, the app\ndoesn't need to keep the screen on but still needs the CPU to be active.\n\nThe approach you take depends on the needs of your app. However, a general rule\nis that you should use the most lightweight approach possible, to minimize your\napp's impact on system resources. This document helps you choose the correct\nAndroid technology for your situation.\n| **Note:** You may be familiar with **wake locks**. An app can set a wake lock to keep the device from suspending. However, using a wake lock can quickly drain the device battery. You should only use a wake lock if there's no other option that will do what you need. If you do use a wake lock, you should release it as soon as possible.\n\nChoose the right technology\n---------------------------\n\nThe best option for keeping your device awake depends on your app's needs. This\nsection helps you choose the right approach.\n\n- Does your app need to keep the screen on?\n - If **Yes** , see [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on). There may be a special-purpose API that does what you need; for example, if you're implementing a phone-call UI, you can use the [Android telecom\n framework](/reference/android/telecom/package-summary), which keeps the screen on when needed. If there's no special purpose API for your situation, you can use the `keepScreenOn` API.\n- Is your app running a foreground service, and you need to keep the device awake when screen is off while the service is running?\n - If **No** , you do not need to keep the device awake. If the user is actively interacting with the app, the device will stay awake. If the user is not interacting with your app and you are not running a foreground service, you should let the device go into suspend mode when necessary. If you just need to make sure some work gets done while the user is away from the app, see the [background tasks](/develop/background-work/background-tasks) documentation to find the best option.\n - If **Yes** , first confirm that you actually need to use a foreground service. Depending on your situation, there may be some special-purpose API you can use to accomplish your need instead of a foreground service. You can find information about these [in the Foreground Service\n documentation](/develop/background-work/services/fgs/service-types). For example, if you need to track the user's location, you might be able to use the [geofencing API](/develop/sensors-and-location/location/geofencing) instead of a `location` foreground service.\n- Would it be detrimental to the user experience if the device suspends while the foreground service is running and the device screen is off? (For example, if you're using a foreground service to update notifications, it wouldn't be a bad user experience if the device is suspended.)\n - If **No**, do not use a wakelock. The action resumes automatically once the user engages with their device, which takes it out of suspend.\n - If **Yes** , you might need to [use a wake lock](/develop/background-work/background-tasks/awake/wakelock). However, you should still check to see if you're already using an API or doing an action that declares a wake lock on your behalf, as discussed in [Actions that keep the device awake](#actions-keep).\n\nActions that keep the device awake\n----------------------------------\n\nIf your app is doing any of the following, you don't need to set a wake lock\nyourself. The following actions and APIs all keep the device awake for you.\n\n- If you're playing audio, the audio system sets and manages a wake lock for you; you don't need to do it yourself.\n- If you're using task scheduling APIs or libraries such as [WorkManager](/develop/background-work/background-tasks/persistent), [`JobScheduler`](/reference/android/app/job/JobScheduler), or [`DownloadManager`](/reference/android/app/DownloadManager), the system or library acquires a wake lock that is attributed to your app.\n- If you're using [Media3 ExoPlayer](/media/media3/exoplayer), you can use [`ExoPlayer.setWakeMode()`](/reference/androidx/media3/exoplayer/ExoPlayer#setWakeMode(int)) to have the player set a wake lock for you.\n- Certain device sensors are wake-up sensors; you can use [`SensorManager`](/reference/android/hardware/SensorManager) to have those sensors wake up the device when they have data to report. To check if a sensor is a wake-up sensor, call [`Sensor.isWakeUpSensor`](/reference/android/hardware/Sensor#isWakeUpSensor())).\n\nSee also\n--------\n\n- [Use wake locks](/develop/background-work/background-tasks/awake/wakelock)\n- [Keep the screen on](/develop/background-work/background-tasks/awake/screen-on)"]]