【问题标题】:About Xamarin Camera permission关于 Xamarin 相机权限
【发布时间】:2017-12-11 08:21:30
【问题描述】:

我正在使用 Xamarin 制作 Android 应用,这使用 zxing。

当用户点击一个按钮时,它会显示 QrScan 页面和对话框,询问是否允许摄像头权限。

我想在每次点击按钮时显示询问用户权限的对话框。

现在,如果用户单击拒绝,则在重新启动应用程序之前不会显示权限对话框。

你有什么想法吗?

这是我的来源。

Android --- MainActivity.cs

 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        LoadApplication(new App { OSVersion = "Android Version " + "2.0" });
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        // If this is not be, occur unexpected exception when user click deny
        if(grantResults[0] == Permission.Denied)
        {
            return;
        }
        global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

这是我在 PCL 项目中执行 QrScan 方法,它调用了单击按钮。

    public async void ImgQrScan_Clicked(object sender, EventArgs e)
    {
        this.TappedEvent?.Invoke(sender, e);
        CustomScanViewMaker();
        await Navigation.PushModalAsync(oCustomQrScanPage);

        zxingPage.IsScanning = true;
        string sScanResult = "";
        zxingPage.OnScanResult += (result) =>
        {
            sScanResult = result.Text;
            zxingPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                this.OnClicked?.Invoke(sender, new QrScannerClickEventArgs(sScanResult));

                await Navigation.PopModalAsync();
            });
        };

        this.OnClicked?.Invoke(sender, new QrScannerClickEventArgs(sScanResult));
    }

谢谢。

【问题讨论】:

  • 这个问题你解决了吗?

标签: xamarin.android zxing


【解决方案1】:

我想在每次点击按钮时显示询问用户权限的对话框。

您可以尝试使用shouldShowRequestPermissionRationale 方法来实现此功能,正如document 所说:

为了帮助找到用户可能需要解释的情况,Android 提供了一个实用方法,shouldShowRequestPermissionRationale()。 如果应用之前已请求此权限并且用户拒绝了该请求,则此方法返回 true。

其用法可以参考官方文档Requesting Permissions at Run Time,在C#中是这样的:

// Here, this is the current activity
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) != Permission.Granted)
{
    // Should we show an explanation?
    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.

       // Show an explanation to the user *asynchronously* -- don't block
       // this thread waiting for the user's response! After the user
       // sees the explanation, try again to request the permission.

       Log.Info(TAG, "Displaying camera permission rationale to provide additional context.");
    }
    else
    {
        // No explanation needed, we can request the permission.
        ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.Camera }, REQUEST_CAMERA);

        // REQUEST_CAMERA is an app-defined int constant. The callback method gets the
        // result of the request.
    }
}
else
{
    System.Diagnostics.Debug.WriteLine("Permission Granted!!!");
}

【讨论】:

  • 感谢回复,抱歉迟到了。我已经尝试过了,但这并不能解决我的问题。通过重新启动应用程序只会出现一次询问许可的仍然对话框。
  • @user5949771,您是否尝试过在您的项目中使用这些代码?你能发布你的代码吗?我认为我们可能需要查看一些代码来帮助您查明问题。
  • 是的,我试过了。对不起,我现在工作太忙了,但只要更新至少在 12 月制作示例源(不能使用公司的源)。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
相关资源
最近更新 更多