gpt4 book ai didi

android - 如何更改 Android M 中的拒绝/授予权限?

转载 作者:太空狗 更新时间:2023-10-29 16:30:45 25 4
gpt4 key购买 nike

我怎样才能让用户有机会更改他在应用程序中的权限,他已经设置为授予/拒绝?

假设用户拒绝了权限。后来他想授予此权限。是否可以?我该怎么做?

最佳答案

如果用户拒绝了权限,您可以检查权限,如果他也检查了不再显示,在这种情况下,您可以打开自己的对话框,解释您的应用程序需要该权限,然后从该对话框中您可以将用户带到应用程序设置,如果他愿意,他可以在其中允许权限。

public static void checkPermissionForExternalStorage(final Context mContext) {

if (Build.VERSION.SDK_INT >= 23) {

int writeExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS_FOR_STORAGE);
return;
}

//Do your stuff

} else {

//Do your stuff
}
}



public static void startInstalledAppDetailsActivity(Context mContext) {

Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + mContext.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(i);
}



@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

switch (requestCode) {



case REQUEST_CODE_ASK_PERMISSIONS_FOR_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do your stuff
} else {
// Permission Denied

if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission. Tap Settings > Permissions, and turn Storage on.",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startInstalledAppDetailsActivity((Activity) mContext);
}
});
}


Toast.makeText( mContext, "Write to external storage permission request denied.", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

关于android - 如何更改 Android M 中的拒绝/授予权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510851/

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