我正在尝试在Android中设置位置权限,当我在设备中自动安装应用程序时,设备会为应用程序分配位置权限(使用ManifestFile)。如果我手动禁用该位置的权限&再次运行应用程序它没有显示任何弹出窗口(我编程要求)。
private const int LOCATION_GROUP_PERMISSION_REQUEST = 1;
if ((int)Build.VERSION.SdkInt > 22) {
if (ContextCompat.CheckSelfPermission (this, Android.Manifest.Permission_group.Location) != Android.Content.PM.Permission.Granted) {
Toast.MakeText (this, "Something Really wrong", ToastLength.Short).Show ();
var data = ActivityCompat.ShouldShowRequestPermissionRationale (this, Manifest.Permission_group.Location);
if (!data) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder (this);
builder.SetTitle ("Location Permission is Disabled");
builder.SetMessage ("Location permission is needed ");
builder.SetCancelable (false);
builder.SetPositiveButton ("Enable", delegate {
ActivityCompat.RequestPermissions (this, new String [] { Manifest.Permission_group.Location },
LOCATION_GROUP_PERMISSION_REQUEST);
});
builder.Show ();
} else {
ActivityCompat.RequestPermissions (this, new String [] { Manifest.Permission_group.Location },
LOCATION_GROUP_PERMISSION_REQUEST);
}
} else {
GoToActivity ();
}
}
数据变量总是返回false
public override void OnRequestPermissionsResult (int requestCode, string [] permissions, Android.Content.PM.Permission [] grantResults)
{
if (requestCode == LOCATION_GROUP_PERMISSION_REQUEST) {
for (int i = 0; i < permissions.Length; i++) {
if (grantResults [i] == Android.Content.PM.Permission.Granted) {
Toast.MakeText (this, "Param granted", ToastLength.Short).Show ();
} else if (grantResults [i] == Android.Content.PM.Permission.Denied) {
Toast.MakeText (this, "param Denied", ToastLength.Short).Show ();
}
}
} else {
base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
}
}
//权限长度为零
答案 0 :(得分:2)
运行时权限仅来自Api等级23及以上 我知道这个答案不是你问题的完美答案,我用原生语回答,所以至少它可以帮助你把它转换成xamarin。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// permissions have not been granted.
requestPermissions();
}
}
}
private void requestPermissions() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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 request has been denied previously.
// this is my custom dialog change as per ur requirement to notify user
showPermissionRationaleDialog("Locations permissions are needed to demonstrate access.", PERMISSIONS_LOCATION, false);
//when user click ok in dialog you have to call requestForPermission method
} else {
// permissions have not been granted yet. Request them directly.
requestForPermission(PERMISSIONS_LOCATION);
}
}
//请求生成
private void requestForPermission(final String[] permissions) {
ActivityCompat.requestPermissions(NwSelectionActivity.this, permissions, REQUEST_CODE);
}
//结果处理
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted,
} else {
boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
permissions[0]);
Log.i(TAG, "Locations permissions were NOT granted.");
if (!showRationale) {
//here you show dialog to user to manually enable location permission in setting
showPermissionRationaleDialog("Allow App to access your locations. Tap Setting > Permissions, and turn Location on.", PERMISSIONS_LOCATION, true);
}
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 1 :(得分:0)
检查权限是否被拒绝或授予 onRequestPermissionsResult。
如果以前拒绝了许可,这次会有一个 请勿在权限对话框中再次询问复选框。
调用shouldShowRequestPermissionRationale以查看用户是否已选中 永远不要再问了。 shouldShowRequestPermissionRationale方法返回 仅当用户选择“永不再询问”或设备策略时才为false 禁止该应用获得该许可: