gpt4 book ai didi

Android运行时权限不可访问

转载 作者:行者123 更新时间:2023-11-29 19:27:19 25 4
gpt4 key购买 nike

当我在设备中自动安装应用程序时,我正在尝试在 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);
}

}

//权限长度为零

最佳答案

运行时权限仅来自 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);
}
}

关于Android运行时权限不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com