Request location permission Android 11 (SDK 30)

Best practices for requesting user location permission from user, including background location.

XD
2 min readFeb 20, 2021

--

To further protect user privacy, Android 11 adds one-time location access and changes how users grant background location access. These updates affect all apps that run on Android 11 and higher.

Best practices that is suggested by Google is to perform incremental requests.

  • At first, your app should guide users to the features that require foreground location access

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.

So, what we can do is, We can first request foreground access from user then go for background, Let’s code 🎸

  • Before we start, You should have this permissions in manifest.

NOTE: We added a new permission ACCESS_BACKGROUND_LOCATION which was added for you before android 10.

  • Now, as per docs we will request foreground_loaction, then background_location access first, So add this method to you project MainActivity.java.

Well Done 🎁

Now, we are almost done with the process, just call checkLocationPermission() whenever, you need to call permission check.

--

--

XD
XD

Written by XD

Code by day, hack the system by night. Navigating the cloud like a neon-lit alleyway. APIs, bugs, and glitches – I fix what the future breaks.

No responses yet