Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini membahas hal-hal berikut:
Cara meminta akses lokasi latar belakang.
Cara menangani permintaan berdasarkan versi SDK target aplikasi Anda.
Cara preferensi pengguna untuk perkiraan lokasi memengaruhi cara aplikasi Anda mendapatkan
lokasi latar belakang.
Gambar 7. Halaman setelan yang menyertakan opsi
Izinkan sepanjang waktu, yang memberikan akses lokasi latar
belakang.
Konten dialog izin bergantung pada versi SDK target
Saat fitur di aplikasi Anda meminta lokasi latar belakang pada perangkat yang menjalankan
Android 10 (API level 29), dialog izin sistem akan menyertakan opsi
bernama Izinkan sepanjang waktu. Jika pengguna memilih opsi ini, fitur di
aplikasi Anda akan mendapatkan akses lokasi latar belakang.
Di Android 11 (API level 30) dan yang lebih tinggi, dialog sistem tidak
menyertakan opsi Izinkan sepanjang waktu. Sebaliknya, pengguna harus mengaktifkan lokasi
latar belakang pada halaman setelan, seperti yang ditunjukkan pada gambar 7.
Anda dapat membantu pengguna membuka halaman setelan ini dengan mengikuti praktik terbaik
saat meminta izin lokasi latar belakang. Proses pemberian izin bergantung pada
versi SDK target aplikasi.
Aplikasi menargetkan Android 11 atau yang lebih baru
Jika aplikasi Anda belum diberi izin ACCESS_BACKGROUND_LOCATION, dan
shouldShowRequestPermissionRationale() menampilkan true, tampilkan
UI edukasi kepada pengguna yang menyertakan informasi berikut:
Penjelasan lengkap tentang alasan fitur aplikasi memerlukan akses ke lokasi latar
belakang.
Label opsi setelan yang dapat dibaca pengguna dan memberikan lokasi
latar belakang (misalnya, Izinkan sepanjang waktu dalam gambar 7). Anda dapat memanggil
getBackgroundPermissionOptionLabel() untuk mendapatkan label ini. Nilai hasil dari metode ini dilokalkan ke preferensi bahasa perangkat pengguna.
Opsi bagi pengguna untuk menolak izin. Jika pengguna menolak akses lokasi
latar belakang, mereka tetap harus dapat menggunakan aplikasi.
Gambar 8. Notifikasi mengingatkan pengguna bahwa mereka telah memberikan
akses lokasi latar belakang ke suatu aplikasi.
Aplikasi yang menargetkan Android 10 atau yang lebih rendah
Saat fitur di aplikasi meminta akses lokasi latar belakang, pengguna akan melihat
dialog sistem. Dialog ini menyertakan opsi untuk membuka opsi izin lokasi
aplikasi di halaman setelan.
Selama aplikasi Anda sudah mengikuti praktik terbaik untuk meminta izin akses
lokasi, Anda tidak perlu membuat perubahan untuk mendukung perilaku ini.
Pengguna dapat memengaruhi akurasi lokasi latar belakang
Jika pengguna meminta perkiraan lokasi, pilihan pengguna dalam
dialog izin akses lokasi juga berlaku untuk lokasi latar belakang. Dengan kata lain,
jika pengguna memberi aplikasi izin ACCESS_BACKGROUND_LOCATION, tetapi
hanya memberikan akses perkiraan lokasi di latar depan, aplikasi Anda hanya memiliki
akses perkiraan lokasi di latar belakang.
Referensi lainnya
Untuk mengetahui informasi selengkapnya tentang izin akses lokasi di Android, lihat materi
berikut:
Aplikasi contoh
untuk menunjukkan penggunaan izin akses lokasi.
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-27 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-27 UTC."],[],[],null,["This page discusses the following:\n\n- How to request background location access.\n- How to handle the request based on your app's target SDK version.\n- How user preferences for approximate location affect how your app gets background location.\n\n**Note:** If a feature in your app accesses location from the background, verify that access is necessary. Consider getting the information that the feature needs in other ways. To learn more about background location access, see the [Access location in the background](/training/location/background) page. \n**Figure 7.** Settings page includes an option called **Allow all the time**, which grants background location access.\n\nPermission dialog contents depend on target SDK version\n\nWhen a feature in your app requests background location on a device that runs\nAndroid 10 (API level 29), the system permissions dialog includes an option\nnamed **Allow all the time**. If the user selects this option, the feature in\nyour app gains background location access.\n\nOn Android 11 (API level 30) and higher, however, the system dialog doesn't\ninclude the **Allow all the time** option. Instead, users must enable background\nlocation on a settings page, as shown in figure 7.\n\nYou can help users navigate to this settings page by following best practices\nwhen requesting the background location permission. The process for granting the\npermission depends on your app's target SDK version.\n\nApp targets Android 11 or higher\n\nIf your app hasn't been granted the `ACCESS_BACKGROUND_LOCATION` permission, and\n[`shouldShowRequestPermissionRationale()`](/reference/androidx/core/app/ActivityCompat#shouldShowRequestPermissionRationale(android.app.Activity,%20java.lang.String)) returns `true`, show an\neducational UI to users that includes the following:\n\n- A clear explanation of why your app's feature needs access to background location.\n- The user-visible label of the settings option that grants background location (for example, **Allow all the time** in figure 7). You can call [`getBackgroundPermissionOptionLabel()`](/reference/android/content/pm/PackageManager#getBackgroundPermissionOptionLabel()) to get this label. The return value of this method is localized to the user's device language preference.\n- An option for users to decline the permission. If users decline background location access, they should be able to continue using your app.\n\n**Figure 8.**Notification reminding the user that they've granted background location access to an app.\n\nApp targets Android 10 or lower\n\nWhen a feature in your app requests background location access, users see a\nsystem dialog. This dialog includes an option to navigate to your app's location\npermission options on a settings page.\n\nAs long as your app already follows best practices for requesting location\npermissions, you don't need to make any changes to support this behavior.\n\nUser can affect background location accuracy\n\nIf the [user requests approximate location](/develop/sensors-and-location/location/permissions/runtime#approximate-request), the user's choices in the\nlocation permissions dialog also apply to background location. In other words,\nif the user grants your app the `ACCESS_BACKGROUND_LOCATION` permission but\ngrants only approximate location access in the foreground, your app has only\napproximate location access in the background as well.\n\nAdditional resources\n\nFor more information about location permissions in Android, view the following\nmaterials:\n\nCodelabs\n\n- [Privacy best practices](/codelabs/android-privacy-codelab)\n\nVideos\n\n- [How to find possible background location usage](https://www.youtube.com/watch?v=xTVeFJZQ28c)\n\nSamples\n\n- [Sample app](https://github.com/android/platform-samples/tree/main/samples/location/src/main/java/com/example/platform/location/permission) to demonstrate the use of location permissions."]]