User Location in Android 11

Updated 11 October 2021


As the new changes are being done for the better privacy of the user Android has updated the permission policy for location access. Android restricts its usage, especially in the background.

Before Android 9, there was no separate location permission for the Foreground and background apps therefore they were using the same resources due to this some of the apps were exploiting the location data of the users.

On Android 11 and higher, whenever your app requests access to the foreground location, the system permissions dialog includes an option called Only this time. This option gives users more control over when an app can access location information.

There are now two options available for the developer to pick from:

1: Foreground Location.

2: Background Location.

Foreground location

When you are developing an app that requires only that share or receives location information only one time, or for a specific amount of time then, that feature requires foreground location access.

To achieve this, we need to add the uses permission tag in the Android Manifest file.

From Android 11, the users now have 3 options to give access to their location:

1: While using the app

2: Only this time

3: Deny

To know the permission added on the Android manifest file are:

ACCESS_COARSE_LOCATION: Provides an estimate of the device location, within about 1 mile (1.6 km).

ACCESS_FINE_LOCATION: Provides an estimate of the device’s location as accurately as possible, usually within about 160 feet (50 meters) and sometimes as accurate as within 10 feet (a few meters) or better.

To get the current location using the foreground location with the help of an example.

Background location

An app requires background location access if a feature within the app constantly shares a location with other users or uses the Geofencing API. Like sharing your live location.

From Android 10 (API level 29) and higher API level, you must have to declare the ACCESS_BACKGROUND_LOCATION permission in your app’s manifest to request background location access at runtime, otherwise, the system will ignore it. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.

The second one is that the application needs to use location also in the background. For which you have to create a dialog with a precise message to explain the use of background location.

After the user agrees, redirect to the application’s setting page where you can choose the permission type as given below.

1: Allow all the time.

2: Allow only while using the app.

3: Ask every time

4: Deny

Now how to ask for the background location permission in code here is the below example.

You can call the backgroundLocationPermission() method on the button click if you want, like the below code.

I have used a simple layout design.

The Result

You can also check out the official documentation provided by Google click here.

. . . . . . . . .

That’s it from my side for today, thanks for reading it until now. You can check our other android blogs Click Here.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home