Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Essentials - Geolocation. Always on permission on Android doesn't work anymore

dai15002dai15002 Member ✭✭✭

After updating the Xamarin libraries in my xamarin forms app and setting the Android target framework from 8.0 to 10.0, the app on Android is no longer requesting Always On location when calling the geolocation code below:

var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10)));

The app only shows the "When app is in use" and "Deny access" option now and no longer the "Always On" option.

I tried adding a new flag permission flag to the android manifest file but no result:

Anyone knows how to solve this and bring back the always on for android devices?

Best Answer

  • dai15002dai15002 Member ✭✭✭
    Accepted Answer

    my solution for now is to ask for location permission "Allow while using the app" using the GetLocationAsync method. If the user grants the permission, I then call for background permission on the next location check in my MainActivity.cs using the code background permission code below:
    ActivityCompat.RequestPermissions(CrossCurrentActivity.Current.Activity, new[] { Manifest.Permission.AccessBackgroundLocation.ToString() }, 1000);

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    I set the Android target framework from 8.0 to 10.0. and add following permission.

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    I got the following result. You can click the allow the grand the permission. if you can try to uninstall your application in this device, re-install it, if have this issue.

  • dai15002dai15002 Member ✭✭✭
    edited October 23

    I did reinstall and it didn't help. I have had those two permissions that you mentioned (the uses-permission strings) in my app since before, and the app used to show a dialog with 3 options. Now, after updating the libs and using the same code, it only shows two options. Those two options are not "Allow" and "Deny" like seen in your screenshot. For me it shows "Allow only when the app is in use" and "Deny".

    I believe the screenshot is taken from an Android OS version less than 10.0 that is why only two options "Allow" (which acts for both foreground and background location check) and "Deny" appear.

    My app is ment to check the users' location even if the app is in background. This has worked all fine before.
    It's hard for me to think that the option "Always On/Always Allow" is somehow deprecated or something.

  • dai15002dai15002 Member ✭✭✭

    Update:
    After digging into this I found out that the problem occurs due to changing the android:targetSdkVersion from 28 to 29. Doing so causes the Xamarin Essential method Geolocation.GetLocationAsync to not show the option "Always Allow"

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited October 27

    Based on your description, you use Android 10, and you need to request ACCESS_BACKGROUND_LOCATION, then open the application in settings, you can set it allow all the time

    It is Android 10 new limitation, you can refer to the following link.
    https://developer.android.com/training/location/permissions?hl=fr#request-background-location

  • dai15002dai15002 Member ✭✭✭

    Since my app is targeting v10, regarding the documentations that you sent saying:
    "When a feature in your app requests background location on a device that runs Android 10 (API level 29), the system permissions dialog includes an option named Allow all the time. If the user selects this option, the feature in your app gains background location access."'

    meaning after I have implemented the ACCESS_BACKGROUND_LOCATION in the manifest, I should be able to ask for "Allow All the time" in the location permission request dialog. However using the essential method "var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10)));", it does not bring the background location option in the dialog.

  • dai15002dai15002 Member ✭✭✭
    Accepted Answer

    my solution for now is to ask for location permission "Allow while using the app" using the GetLocationAsync method. If the user grants the permission, I then call for background permission on the next location check in my MainActivity.cs using the code background permission code below:
    ActivityCompat.RequestPermissions(CrossCurrentActivity.Current.Activity, new[] { Manifest.Permission.AccessBackgroundLocation.ToString() }, 1000);

Sign In or Register to comment.