Overview
Restricted location permission in Android R
Developers often get confused with ever-changing location usage policy, let’s clear that fog in this blog.
Changelog
Google has been working on user’s privacy for the last few years, as a part of security and privacy they restricted location usage in android applications. Let’s take a look at changelog in android versions for location usage.
Android 8 (Oreo): Apps accessing location in background will receive location updates a few times each hour.
Android 10 (Q): New ACCESS_BACKGROUND_LOCATION permission for the apps using location in background, prior to this version there was a single permission for background and foreground state of application.
Android 11 (R): ACCESS_BACKGROUND_LOCATION is separated from other location permissions, the user has to grant background permission explicitly from the application’s setting page. Developers have to design custom consent dialog for this permission and redirect users to the application’s setting page to grant the permission. Another major change is foreground location service, apps must display a notification indicating ongoing location activity in service, check out “while-in-use restriction” section for implementation.
Let’s take a look at code snippets to better understand how to prompt users for the location permission:
Upto Android 9:
fun requestLocationPermission() { val permList = arrayOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION ) requestPermissions(permList, locationRequestCode) }
Android 10:
Introduced ACCESS_BACKGROUND_LOCATION permission for applications using location service in background.
fun requestLocationPermission() { val permList = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION) requestPermissions(permList, locationRequestCode) }
Android 11:
Foreground permissions are requested as in Android 9 and lower versions. But, for background location you have to design a custom dialog asking for background location permission and redirect the user to the application’s settings page. Your app will receive location updates only a few times each hour.
fun requestBackgroundPermission() { val backPermList = arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION) AlertDialog.Builder(this) .setTitle("Background location permission") .setMessage("Allow location permission to get location updates in background") .setPositiveButton("Allow") { _, _ -> requestPermissions( backPermList, backLocationRequestCode ) } .setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() } .create() .show() }
Note that requestPermissions() will redirect users to the application’s settings page on Android 11 devices. And for older versions you can create setting’s intent and startActivity.
While-in-use restriction
On Android R (API 30), it is best practice to use foreground service if your app wants to access location in background. You have to define service type to Location in AndroidManifest file as follow:
<manifest> <service ... android:foregroundServiceType="location|camera|microphone" /> </manifest>
And, start service like:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) startForeground(id, notification, FOREGROUND_SERVICE_TYPE_LOCATION) else startForeground(id, notification)
Permissions for while-in-use:
- Your app needs ACCESS_BACKGROUND_LOCATION permission to use location in foreground service.
- CAMERA permission, if foreground service is using camera features.
- RECORD_AUDIO permission, if foreground service is using microphone features.
PlayStore Policy
According to guidelines, your application should not request background location permission unless it is required. In case your app is using background location permission then you have to submit a Location Permission Declaration Form to approve your app on play store. Check out Play Console and follow all guidelines to get quick approval.
Wrap up
It is not like you can not use background location from android 11, users have to give background location permission from application settings.