Android Location Permissions
AreaMetrics Snippet requires the ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions if targeting Android 10+.
Targeting Android API 29 and above
Add the ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions to your AndroidManifest.xml, like so:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Ask for location permission on a screen in your app where the user will understand the context and be receptive to agreeing. It is a good idea to specify to the user that "Allow all the time" permission is preferred before presenting the prompt.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 1);
}
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
Alternatively, Google provides a good example of how to ask for the location permission and background permission in a defined sequence. It is up to your app to implement an approach that you believe will work best. Please see the code example here: https://developer.android.com/training/location/receive-location-updates#request-background-location
Targeting Android API 28 and below
Our Altbeacon dependency already adds ACCESS_COARSE_LOCATION to your AndroidManifest.xml, so all that's left is the code for showing the runtime permission prompt. Ask for location permission at a moment in your app the user will understand such as after on-boarding or in a specific area of your app.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
Last updated