To protect user privacy, apps that use location services must request location permissions.
When you request location permissions, follow the same best practices as you would for any other runtime permission. One important difference when it comes to location permissions is that the system includes multiple permissions related to location. Which permissions you request, and how you request them, depend on the location requirements for your app's use case.
This page describes the different types of location requirements and provides guidance on how to request location permissions in each case.
Types of location access
Android's location permissions deal with the following categories of location access:
This section describes the situations when your app uses each category.
If your app contains a feature that shares or receives location information only once, or for a defined amount of time, then that feature requires foreground location access. Some examples include the following:
- Within a navigation app, a feature allows users to get turn-by-turn directions.
- Within a messaging app, a feature allows users to share their current location with another user.
The system considers your app to be using foreground location if a feature of your app accesses the device's current location in one of the following situations:
- An activity that belongs to your app is visible.
Your app is running a foreground service. When a foreground service is running, the system raises user awareness by showing a persistent notification. Your app retains access when it's placed in the background, such as when the user presses the Home button on their device or turns their device's display off.
Additionally, it's recommended that you declare a foreground service type of
location, as shown in the following code snippet. On Android 10 (API level 29), you must declare this foreground service type.
<!-- Recommended for Android 9 (API level 28) and lower. --> <!-- Required for Android 10 (API level 29). --> <service android:name="MyNavigationService" android:foregroundServiceType="location" ... > <!-- Any inner elements would go here. --> </service>
<manifest ... > <!-- To request foreground location access, declare one of these permissions. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
The level of precision depends on which permission you request:
- Provides location accuracy to within a city block.
- Provides a more accurate location than one provided when you request
ACCESS_COARSE_LOCATION. This permission is necessary for some connectivity tasks, such as connecting to nearby devices over Bluetooth Low Energy (BLE).
An app requires background location access if a feature within the app constantly shares location with other users or uses the Geofencing API. Several examples include the following:
- Within a family location sharing app, a feature allows users to continuously share location with family members.
- Within an IoT app, a feature allows users to configure their home devices such that they turn off when the user leaves their home and turn back on when the user returns home.
The system considers your app to be using background location if it accesses the device's current location in any situation other than the ones described in the foreground location section.
On Android 10 (API level 29), you must declare the
permission in your app's manifest in order to request background location access
at runtime. On earlier versions of Android, when your app receives foreground
location access, it automatically receives background location access as well.
<manifest ... > <!-- Required only when requesting background location access on Android 10 (API level 29). --> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> </manifest>
Request location access at runtime
When a feature in your app needs location access, wait until the user interacts with the feature before making the permission request. This workflow follows the best practice of asking for runtime permissions in context, as described in the guide that explains how to request app permissions.
Figure 1 shows an example of how to perform this process. The app contains a "share location" feature that requires foreground location access. The app doesn't request the location permission, however, until the user selects the Share location button.
Even if several features in your app require location access, it's likely that only some of them require background location access. Therefore, it's recommended that your app performs incremental requests for location permissions, asking for foreground location access and then background location access. By performing incremental requests, you give users more control and transparency because they can better understand which features in your app need background location access.
Figure 2 shows an example of an app that's designed to handle incremental requests. Both the "show current location" and "recommend nearby places" features require foreground location access. Only the "recommend nearby places" feature, however, requires background location access.
The process for performing incremental requests is as follows:
At first, your app should guide users to the features that require foreground location access, such as the "share location" feature in Figure 1 or the "show current location" feature in Figure 2.
It's recommended that you disable user access to features that require background location access until your app has foreground location access.
At a later time, when the user explores functionality that requires background location access, you can request background location access. The system displays a dialog similar to the one shown in Figure 3.
When a feature in your app accesses device location in the background for the first time after the user grants background location access, the system schedules a notification to send to the user. This notification reminds the user that they've allowed your app to access device location all the time. An example notification appears in Figure 4.