如何获得相机权限是在Android应用程序的应用程序权限启用?
我正在使用xamarin plateform的android应用程序。我已经从应用程序清单启用了应用程序的相机功能。运行应用程序用户后,从应用程序权限屏幕中禁用相机。那么,我如何才能让该用户从应用程序权限中禁用此功能?如何获得相机权限是在Android应用程序的应用程序权限启用?
我想下面的代码来获取它,但每次我只得到“授予”的结果。如果用户禁用权限,那么我应该得到“拒绝”的结果。
var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);
在Android棉花糖,你需要在运行时要求的权限。您可以使用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
}
申请查看更多详细信息,你需要
如果您的应用程序的权限尚未拥有所需的权限,应用程序必须调用其中一个requestPermissions()方法请求适当的权限。您的应用会传递所需的权限,并且还会指定一个整数请求代码,用于标识此权限请求。此方法异步运行:它立即返回,并且在用户响应对话框后,系统调用应用程序的回调方法和结果,并将相同的请求代码传递给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.
}
}
处理的权限请求响应
当你的应用程序请求的权限,系统会显示一个对话框给用户。当用户响应时,系统调用您的应用程序的OnRequestPermissionsResult()方法,并将其传递给用户响应。您的应用必须重写该方法才能确定是否授予了该权限。该回调将传递给您传递给requestPermissions()的相同请求代码。例如,如果一个应用程序请求访问摄像头可能有以下回调方法
public override void OnRequestPermissionsResult(int requestCode,
string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_Camera: {
// If request is cancelled, the result arrays are empty.
if (grantResults.Length > 0 && grantResults[0] == Permission.Granted) {
// permission was granted, yay! Do the
// camera-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
例如上述基于谷歌原许可documentions
感谢您的答复。我已经安装了它。你知道我应该用什么代码来获得许可结果吗? – anand
感谢您回复Giorgi。但Permission.Camera不可用。在权限下,只有两个值被授权和拒绝可用。我试着用Manifest.Permission.Camera,但它的抛出错误,它是不正确的值,应该在这种方法paas。 – anand
我已经完成了编码部分,但在每种情况下仍然获得“授予”的价值。如果我从应用程序权限屏幕禁用相机权限,那么我仍然得到授予结果。你有什么想法我可能做错了什么? – anand