【问题标题】:Cannot find "ACCESS_BACKGROUND_LOCATION" permission in Xamarin android code. I need to check if user has Granted this permission在 Xamarin android 代码中找不到“ACCESS_BACKGROUND_LOCATION”权限。我需要检查用户是否已授予此权限
【发布时间】:2020-02-12 03:06:31
【问题描述】:

当在 Xamarin 中的 Android-10 设备上询问位置权限时,我正在尝试检查用户是否选择了“仅在使用应用程序时允许”选项。

我已将我的 android sdk 更新到最新版本,并使用所有其他单声道软件包更新了 Visual Studio。我尝试将目标版本设置为 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 中可用。

【问题讨论】:

    标签: android xamarin xamarin.android android-permissions android-location


    【解决方案1】:

    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_LOCATIONACCESS_COARSE_LOCATION,系统会在构建过程中自动添加ACCESS_BACKGROUND_LOCATION权限。

    同样,如果应用请求ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION,系统会自动将ACCESS_BACKGROUND_LOCATION添加到请求中。

    【讨论】:

    • 我想这就是我的问题的答案。关于 Microsoft 何时更新此支持库的任何想法?任何指向预发布说明的链接?
    • 因为Android Q还处于测试阶段。支持SDK将在Android Q正式发布时更新。我相信它不会让你久等的。你可以接受我的回答,这将帮助更多的人:)
    • 他们会在 v28.0.0.3 上提供更新,还是我们需要在 AndroidX 上迁移?
    • 如果我的帖子能帮助您解决问题,您可以将我的帖子标记为答案。
    • 这个答案是错误的,但被用于问这个问题的每个人。参考。官方文档:docs.microsoft.com/en-us/xamarin/android/platform/androidx此包不再支持,永远不会更新。
    【解决方案2】:

    对于完全相同的事情,我有一个可行的解决方案,如下所示:

    在您的 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();
                }
            }
        }
    

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

    【讨论】:

      【解决方案3】:

      我认为您需要处理 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 的专业人士,但我认为这可能是一个适合您的解决方案,并且您还可以使用此插件检查用户是否授予权限

      【讨论】:

      • 这不是我的问题的答案。我需要在代码中使用“ACCESS_BACKGROUND_LOCATION”这个特殊权限
      猜你喜欢
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多