如何在Android应用程序的应用程序权限中启用相机权限?

时间:2016-04-21 09:59:10

标签: android xamarin xamarin.android xamarin.forms

我正在使用xamarin平台的Android应用程序。我已从应用清单中为应用启用了相机功能。运行应用程序用户后,从应用程序权限屏幕禁用相机。那么我如何让该用户从app权限中禁用此功能?

我正在尝试使用代码来获取它,但每次我只获得“Granted”结果。如果用户禁用了权限,那么我应该在结果中“拒绝”。

 var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);

enter image description here

2 个答案:

答案 0 :(得分:4)

请求您需要的权限

如果您的应用尚未拥有所需的权限,则应用必须调用其中一个 requestPermissions()方法来请求相应的权限。您的应用程序会传递它想要的权限,以及您指定用于标识此权限请求的整数请求代码。该方法异步运行:它立即返回,在用户响应对话框后,系统调用应用程序的回调方法和结果,传递应用程序传递给 requestPermissions()的相同请求代码 *

continue;

处理权限请求响应

当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的OnRequestPermissionsResult()方法,并向其传递用户响应。您的应用必须覆盖该方法才能确定是否已授予权限。回调传递给您传递给requestPermissions()的相同请求代码。例如,如果应用请求相机访问权限,则可能具有以下回调方法

int MY_PERMISSIONS_REQUEST_Camera=101;
// Here, thisActivity is the current activity
if (ContextCompat.CheckSelfPermission(thisActivity,
                Manifest.Permission.Camera)
        != Permission.Granted) {

    // Should we show an explanation?
    if (ActivityCompat.ShouldShowRequestPermissionRationale(thisActivity,
            Manifest.Permission.Camera)) {

        // Show an expanation 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.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.RequestPermissions(thisActivity,
                new String[]{Manifest.Permission.Camera},
                MY_PERMISSIONS_REQUEST_Camera);

        // MY_PERMISSIONS_REQUEST_Camera is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

以上示例基于Google原始权限documentions

答案 1 :(得分:0)

在Android Marshmallow上,您需要在运行时请求权限。您可以使用Permissions Plugin for Xamarin来提示您所需的权限。

Requesting Runtime Permissions in Android Marshmallow

了解详情

以下是一个例子:

var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
if (status == PermissionStatus.Granted)
{
    //Permission was granted
}

您可以在Permissions Plugin for Xamarin ReadMe

查看更多详情