Xamarin Android-API 27 RequestPermission

时间:2018-10-23 11:48:35

标签: xamarin.android android-permissions

我正在使用以下代码来请求FineLocation / CoarseLocation和相机的权限:

protected override void OnResume()
        {
            base.OnResume();
            txt_TrashSubmitDescriptionTitle = FindViewById<TextView>(Resource.Id.txt_TrashSubmitDescriptionTitle);

            var c = ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == (int)Permission.Granted;
            var y = ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) == (int)Permission.Granted;
            var z = ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == (int)Permission.Granted;

            if (c && y && z)
            {
                StartLocationService();


            }
            else
            {
                var requiredPermissions = new string[] { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation, Manifest.Permission.Camera };
                if (GeneralFunctions.CheckShouldShowRequestPermissionRationaleForPermissions(this, requiredPermissions))
                {
                    // 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.

                    Snackbar.Make(txt_TrashSubmitDescriptionTitle,
                                   Resource.String.permission_location_rationale,
                                   Snackbar.LengthIndefinite)
                            .SetAction(Resource.String.ok, new Action<Android.Views.View>(delegate (Android.Views.View x) { ActivityCompat.RequestPermissions(this, requiredPermissions, 2); })).Show();
                }
                else
                {
                    ActivityCompat.RequestPermissions(this, requiredPermissions, 2);
                }
            }
        }

此功能用于检查是否应显示PermissionRadiale:

public static bool CheckShouldShowRequestPermissionRationaleForPermissions(Activity _activity, string[] requiredPermissions)
        {
            bool returnBool = false;
            foreach(string _permission in requiredPermissions)
            {
                returnBool = ActivityCompat.ShouldShowRequestPermissionRationale(_activity, _permission);
            }
            return returnBool;
        }

函数OnResume不断被调用,没有特定的错误,我也不知道为什么。

有人知道为什么继续调用OnResume吗?

1 个答案:

答案 0 :(得分:0)

首先,请确保您具有清单中设置的权限:

this.props.imageFilePath

如果没有这些内容,则该应用程序将与OnResume()中的代码一起进入无限的OnResume()循环。至少在我针对Android 9.0的测试应用中做到了。

一旦设置了这些权限,如果用户授予所有特权,代码就可以正常工作。但是,我仍然会将RequestPermissions()代码从OnResume()移到OnCreate()或OnStart()。原因之一是因为用户可以选中“不再询问”,在这种情况下可能导致无休止的循环。

在某些背景下...当显示RequestPermissions()对话框时,您的活动将触发OnPause(),而当关闭RequestPermissions对话框时,将引发OnRequestPermissionsResult()和OnResume()。在激发RequestPermissions()但看不到对话框的情况下,也似乎会发生这种情况。

希望这会有所帮助!