xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

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

Open adamzucchi opened 2 years ago

adamzucchi commented 2 years ago

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" />
  6. Wire up the first button click with the following request for location permission:
    • await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
  7. 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

Reproduction Link

XamFormsAndroidTwelveLocationPoc.zip

adamzucchi commented 2 years ago

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

adamzucchi commented 2 years ago

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

darrabam commented 1 year ago

@adamzucchi Did you find a workaround for this?