在Xamarin Android中请求运行时用户的位置权限时应用崩溃

问题描述 投票:0回答:2

当我移动到另一个选项卡时,我只是请求位置的运行时权限(ACCESS_FINE_LOCATION),那时应用程序崩溃了。

private void RequestPermissions()
        {
            try
            {
                bool shouldProvideRationale = Android.Support.V4.App.ActivityCompat.ShouldShowRequestPermissionRationale(Activity, Manifest.Permission.AccessFineLocation);

                if (shouldProvideRationale)
                {
                    Log.Info(Tag, "Displaying permission rationale to provide additional context.");
                }
                else
                {
                    Android.Support.V4.App.ActivityCompat.RequestPermissions(Activity, new String[] { Manifest.Permission.AccessFineLocation }, 1);
                    Log.Info(Tag, "Requesting permission");
                    // StartLocationPermissionRequest();
                }
            }
}

我需要弹出窗口才会响应该许可。

android xamarin.android request location android-permissions
2个回答
0
投票

你是否在Manifest中声明了许可?如果没有,请转到AndroidManifest.xml并添加:<uses-permission android:name =“android.permission.ACCESS_FINE_LOCATION”/>


0
投票

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

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

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

检查权限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.
                Service.RequestLocationUpdates();
            }
            else
            {
                // Permission denied.
                SetButtonsState(false);
                Toast.MakeText(this, "Permission Denied", ToastLength.Long).Show();
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.