在 Xamarin android 代码中找不到 "ACCESS_BACKGROUND_LOCATION" 权限。我需要检查用户是否已授予此权限

Cannot find "ACCESS_BACKGROUND_LOCATION" permission in Xamarin android code. I need to check if user has Granted this permission

当在 Xamarin 中的 Android-10 设备上请求位置权限时,我正在尝试检查用户是否选择了 "Allow only while app is use" 选项。

我已经将我的 android sdk 更新到最新,并且还用所有其他单声道更新了 visual studio packages.I 已尝试将目标版本设置为 Api 29( Android 10) 但仍然没有运气。

在原生 Android 代码(JAVA 或 Kotlin)中,它简单易用,例如,

val hasBackgroundLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED

我想在 Xamarin 中实现相同的功能 android(C#)

我期待我应该在代码中获得此权限(ACCESS_BACKGROUND_LOCATION)。 但我没有看到这在 Xamarin 中可用。

权限 ACCESS_BACKGROUND_LOCATION 是 Android 10.0 之后的新权限。即使你设置了目标版本为 Api 29 ,但是 Xamarin.Android 中支持的 SDK 版本仍然是 v28.x.x.x (Android 9.0) 。所以这个枚举仍然不可用在 Xamarin.Android 现在。您只需要等待支持SDK的更新即可。

在您的情况下,ACCESS_BACKGROUND_LOCATION 将与旧版本兼容。 如果应用申请了ACCESS_FINE_LOCATION或者ACCESS_COARSE_LOCATION,系统会自动添加一个权限ACCESS_BACKGROUND_LOCATION 正在建设中。

同理,如果申请请求ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION,系统会自动添加ACCESS_BACKGROUND_LOCATION请求。

我认为您需要处理 Xamarin 平台上的权限。类似于:

await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
                {
                    ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
                    AllowBackgroundUpdates = true,
                    DeferLocationUpdates = true,
                    DeferralDistanceMeters = 1,
                    DeferralTime = TimeSpan.FromSeconds(1),
                    ListenForSignificantChanges = true,
                    PauseLocationUpdatesAutomatically = false
                });

我不是 Xamarin 专家,但我认为这可能是适合您的解决方案,并且使用此插件您还可以检查用户是否授予权限

我有一个完全相同的解决方案,如下所示:

在您的 OnCreate 方法中检查现有权限:

 if (!(CheckPermissionGranted(Manifest.Permission.AccessCoarseLocation) 
         &&(CheckPermissionGranted(Manifest.Permission.AccessBackgroundLocation) 
         && CheckPermissionGranted(Manifest.Permission.AccessFineLocation)))
        {
            RequestLocationPermission();
        }
        else
        {
            InitializeLocationManager();
        }
        InitPageWidgets();

其中 Check permission Granted 是这样的方法:

 [Export]
    public bool CheckPermissionGranted(string Permissions)
    {
        // Check if the permission is already available.
        if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
        {
            return false;
        }
        else
        {
            return true;
        }


    }

请求权限代码如下所示:

  private void RequestLocationPermission()
    {
        if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation))
        {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            ActivityCompat.RequestPermissions(this, PermissionsLocation, REQUEST_LOCATION);

        }
        else
        {
            // Camera permission has not been granted yet. Request it directly.
            ActivityCompat.RequestPermissions(this, PermissionsLocation, REQUEST_LOCATION);
        }
    }

一旦您接受或拒绝此权限,将调用此方法:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions,
                     Android.Content.PM.Permission[] grantResults)
    {
        Log.Info(Tag, "onRequestPermissionResult");
        if (requestCode == RequestPermissionsRequestCode)
        {
            if (grantResults.Length <= 0)
            {
                // If user interaction was interrupted, the permission request is cancelled and you
                // receive empty arrays.
                Log.Info(Tag, "User interaction was cancelled.");
            }
            else if (grantResults[0] == PermissionChecker.PermissionGranted)
            {
                // Permission was granted.

            }
            else
            {
                // Permission denied.

                Toast.MakeText(this, "Permission Denied", ToastLength.Long).Show();
            }
        }
    }

请注意,这仅适用于最新版本的 Xamarin VS 2019