Essentials icon indicating copy to clipboard operation
Essentials copied to clipboard

Android 12 Geolocation Request Prompts User to Change from Approximate Location to Precise

Open adamzucchi opened this issue 2 years ago • 3 comments

Description

When targeting Android 12 on a device running Android 12, prompting the user for location permission access has the option for both precise and approximate. If the user selects 'approximate' and then in code Geolocation.GetLocationAsync() is called the user is prompted via the Android OS to switch from approximate to precise location. If another Geolocation.GetLocationAsync() is called after the previous GetLocationAsync() returns, the user is again prompted to switch. This appears to happen twice from what I have seen so far.

Steps to Reproduce

  1. Create a new Xamarin.Forms solution
  2. Update Xamarin.Forms and Xamarin.Essentials to the latest (5.0.0.2401 and 1.7.3 respectively as of the filing of this bug).
  3. Wire up a button that will request the user's permission for LocationWhenInUse
  4. Wire up another button that will be used to retrieve the user's location
  5. Update the AndroidManifest with the following permissions and features:
  • <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • <uses-permission android:name="android.permission.INTERNET" />
  • <uses-feature android:name="android.hardware.location" android:required="false" />
  • <uses-feature android:name="android.hardware.location.gps" android:required="false" />
  • <uses-feature android:name="android.hardware.location.network" android:required="false" />
  1. Wire up the first button click with the following request for location permission:
  • await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
  1. Wire up the second button click with the following user location request:
  • private CancellationTokenSource _cts;//this should be a field at the class level for the code behind file
  • var request = new GeolocationRequest(GeolocationAccuracy.Lowest, TimeSpan.FromSeconds(10));
  • _cts = new CancellationTokenSource();
  • var location = await Geolocation.GetLocationAsync(request, _cts.Token);

Expected Behavior

Upon pressing the first button the user will be prompted to choose between precise or approximate location (select approximate). Then upon pressing the second button the user's location should be retrieved without an OS prompt to change the location permission.

Actual Behavior

Upon pressing the first button the user will be prompted to choose between precise or approximate location (select approximate). Then upon pressing the second button an OS prompt is shown to change the user's location permission from approximate to precise. If this second button is pressed again the prompt is shown again.

Basic Information

  • Version with issue: Xamarin.Essentials 1.7.3
  • Last known good version:
  • IDE: VS 2022 for Mac
  • Platform Target Frameworks:
    • Android: 12
  • Nuget Packages: Xamarin.Forms 5.0.0.2401 & Xamarin.Essentials 1.7.3
  • Affected Devices: Google Pixel 4a running Android 12

Reproduction Link

XamFormsAndroidTwelveLocationPoc.zip

adamzucchi avatar Jun 01 '22 18:06 adamzucchi

My apologies, the Geolocation call in the MainPage.xaml.cs should be GetLocationAsync, not GetLastKnownLocationAsync.

adamzucchi avatar Jun 01 '22 19:06 adamzucchi

Screen recording: http://adamzucchi.com/stage/xamarin/And12LocationPrompt/index.html

adamzucchi avatar Jun 02 '22 15:06 adamzucchi

@adamzucchi Did you find a workaround for this?

darrabam avatar Jun 19 '23 14:06 darrabam