如果用户拒绝权限

时间:2016-08-10 11:42:35

标签: android xamarin.forms android-permissions

我正在使用xamarin表格。当用户安装应用程序时,我需要询问一些权限,例如相机和位置。如果用户“接受”该权限,则OnRequestPermissionsResult方法成功执行。但是如果用户拒绝权限OnRequestPermissionsResult从未调用过,应用程序就会关闭。 当用户拒绝许可时,请更新应用关闭的原因?

    public void RequestCameraPermission()
    {
        try
        {
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.Camera))
            {
                // 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(layout, Resource.String.permission_camera_rationale,
                    Snackbar.LengthIndefinite).SetAction(/*Resource.String.ok*/"OK", new Action<Android.Views.View>(delegate (Android.Views.View obj)
                    {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera }, REQUEST_CAMERA);
                    })).Show();
            }
            else
            {
                // Camera permission has not been granted yet. Request it directly.

                ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera }, REQUEST_CAMERA);
            }
        }
        catch(Exception ex)
        {

        }
    }

  public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        bool cameraPerGranted = false;
        bool micPerGranted = false;
        bool locationPerGranted = false;

        if (requestCode == REQUEST_CAMERA)
        {
            // Received permission result for camera permission.
            // Check if the only required permission has been granted
            if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
            {
                cameraPerGranted = true;
                GlobalClass.isCameraEnabled = cameraPerGranted;
            }
            else
            {
                GlobalClass.isCameraEnabled = false;
            }
        }
        else if (requestCode == REQUEST_MIC)
        {
            // Received permission result for microphone permission.

            // Check if the only required permission has been granted
            if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
            {
                micPerGranted = true;
                GlobalClass.isMicrophoneEnabled = micPerGranted;
            }
            else
            {
                GlobalClass.isMicrophoneEnabled = false;
            }
        }
        else if (requestCode == REQUEST_LOCATION)
        {
            // Received permission result for microphone permission.

            // Check if the only required permission has been granted
            if (grantResults.Length > 0 && grantResults[0] == Permission.Granted && grantResults[1] == Permission.Granted)
            {
                locationPerGranted = true;
                GlobalClass.isLocationEnabled = locationPerGranted;
            }
            else
            {
                GlobalClass.isLocationEnabled = false;
            }
        }
        else
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

此致 阿南德杜比

0 个答案:

没有答案