Search code examples
androidxamarinxamarin.formspermissionslocation

background location not working when app in background xamarin forms android app when upgrade target api to 29


i have xamarin forms app that have tracking and fore ground service for background tracking working fine when set android target api level 28 but after upgrade it target api level 29 to upload app to play store no background location permission set to app so cannot get location when app in background.

and this manifest Permissions:

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

and this run time request in main activity:

     private static string[] _initialPerms ={
        Manifest.Permission.AccessFineLocation,
        Manifest.Permission.AccessCoarseLocation
    };
    
 if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == Permission.Denied || ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == Permission.Denied)
        {
            RequestPermissions(_initialPerms, 1337);
        }

thanks.


Solution

  • The permission ACCESS_BACKGROUND_LOCATION is new after Android 10.0 . Even if you have set the target version to Api 29 , but the version of support SDK in Xamarin.Android is still v28.x.x.x (Android 9.0) .So this enumeration is still unavailable in Xamarin.Android now . What you need is just to wait the update of the support SDK .

    enter image description here

    In your case , ACCESS_BACKGROUND_LOCATION will compatible with old version . If the application apply for ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION, the system will automatically add a permission ACCESS_BACKGROUND_LOCATION during building.

    ===========================Update===============================

    On Android 10 (API level 29) and higher, you must declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.

    <manifest ... >
      <!-- Required only when requesting background location access on
           Android 10 (API level 29) and higher. -->
      <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    </manifest>
    

    More info refer to android document here.