Geofencing on Android 11

Umang Chamaria
3 min readOct 3, 2020

Before getting into how to get Geofence working on Android 11 let’s get an overview of what Geofences are in general and how they work.

What is Geofence?

A geo-fence is a virtual perimeter for a real-world geographic area. A geo-fence could be dynamically generated — as in a radius around a point location, or a geo-fence can be a predefined set of boundaries (such as school zones or neighborhood boundaries). The use of a geo-fence is called geo-fencing, and one example of usage involves a location-aware device of a location-based service (LBS) user entering or exiting a geo-fence. This activity could trigger an alert to the device’s user as well as messaging to the geo-fence operator.

Geofencing combines awareness of the user’s current location with awareness of the user’s proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest.

Refer to the training documentation to learn more about how to set geofences.

Geofencing on Android 11

To use geofencing, your app must request the following:

ACCESS_FINE_LOCATION

ACCESS_BACKGROUND_LOCATION if your app targets Android 10 (API level 29) or higher

As stated in the documentation if your app targets Android 10 or above ACCESS_BACKGROUND_LOCATION is required for geo-fence to work.

On Android 11 (API level 30) and higher, the system dialog doesn’t include the Allow all the time option. Instead, users must enable background location on a settings page.

How to request background permission if does not include it?

If your application hasn’t been granted the ACCESS_BACKGROUND_LOCATION permission, and shouldShowRequestPermissionRationale() returns true, show an educational UI to users that include the following:

  • A clear explanation of why your application’s feature needs access to background location.
  • Get the user-visible label of the settings option that grants background location. You can call getBackgroundPermissionOptionLabel() to get this label. The return value of this method is localized to the user's device language preference.
  • An option for users to decline permission. If users decline background location access, they should be able to continue using your application.

Though it is not necessary to get the label and show it in the educational UI, it is definitely helpful as there are multiple options in the System Settings and the user could easily be confused.

Permission for Geofence

The above-highlighted permission is required for Geofence to work on Android 11.

Once the user agrees to grant the background location permission navigate the user to your application’s Settings/App Info page.

How to navigate the user to the Settings/App Info page?

Use the below code to navigate the user to the App Info page

If the above permission is not given by the user, the application will not be able to set a Geofence.

Important Note

  • If the application requests for ACCESS_BACKGROUND_LOCATION using the ActivityCompat.requestPermissions() on Andriod 11 the system will ignore the request and not show the system dialog to the user requesting permission.
  • As on Android 10, when a feature in your application accesses device location in the background for the first time after the user grants background location access, the system schedules a notification to send to the user. This notification reminds the user that they’ve allowed your app to access device location all the time.

--

--