22nd Feb 2021

Background Location usage in Android 11

Mobile App

Written By, Savan Kashiyani

Overview

Restricted location permission in Android R

background-location-usage
Source: Dribble by Somy Dhyani

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:

background-location-usage-demo

Android 10:

Introduced ACCESS_BACKGROUND_LOCATION permission for applications using location service in background.

background-location-usage-demo-android-10

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.

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.
background-location-usage-demo-android-11

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:

And, start service like:

Permissions for while-in-use:

  1. Your app needs ACCESS_BACKGROUND_LOCATION permission to use location in foreground service.
  2. CAMERA permission, if foreground service is using camera features.
  3. 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.

Written By,

Sr. Android Developer at Yudiz Solutions Pvt. Ltd