如何在android应用程序中启用应用内权限来获取相机权限?
我正在xamarin平台上开发android应用程序。我已从应用程序清单为应用程序启用了摄像头功能。运行应用程序后,用户从应用程序权限屏幕禁用相机。那么,我如何才能让该用户从应用程序权限中禁用此功能 我试着用下面的代码来获得它,但每次我得到的结果都是“被授予”的。如果用户禁用了权限,那么我应该在结果中得到“拒绝”如何在android应用程序中启用应用内权限来获取相机权限?,android,xamarin,xamarin.android,xamarin.forms,Android,Xamarin,Xamarin.android,Xamarin.forms,我正在xamarin平台上开发android应用程序。我已从应用程序清单为应用程序启用了摄像头功能。运行应用程序后,用户从应用程序权限屏幕禁用相机。那么,我如何才能让该用户从应用程序权限中禁用此功能 我试着用下面的代码来获得它,但每次我得到的结果都是“被授予”的。如果用户禁用了权限,那么我应该在结果中得到“拒绝” var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);
var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);
在Android棉花糖上,您需要在运行时请求权限。您可以使用来提示输入所需的权限 阅读更多详细信息,请访问 以下是一个例子:
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()的请求代码相同。例如,如果应用程序请求摄像头访问,它可能具有以下回调方法
以上示例基于谷歌原始权限谢谢回复。我已经安装好了。你知道我应该使用什么代码来获得权限结果吗?谢谢你的回复Giorgi。但请允许。相机不可用。在“权限”下,只有两个“已授予”和“已拒绝”值可用。我尝试了Manifest.Permission.Camera,但它的抛出错误是,在这个方法中,它不是正确的paas值。我已经完成了编码部分,但在每种情况下,我仍然获得“授权”值。如果我从应用程序权限屏幕禁用相机权限,那么我仍然会在结果中获得授权。你知道我做错了什么吗?
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
}
}