我正在xamarin plateform中开发android应用程序。我已经从应用程序清单中启用了应用程序的相机功能。运行应用程序后,用户在应用程序权限屏幕中禁用摄像头。那么,如何才能让用户从应用程序权限中禁用此功能呢?
我正在尝试跟随代码来获取它,但每次我在result中都只能得到“授权”。如果用户禁用权限,那么我应该在结果中得到“拒绝”。
var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);

发布于 2016-04-21 18:17:37
请求您需要的权限
如果您的应用程序还没有所需的权限,则应用程序必须调用其中一个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的
发布于 2016-04-21 18:00:14
在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上查看更多详细信息
https://stackoverflow.com/questions/36766143
复制相似问题