如何更改Android M中的拒绝/授予权限? [英] How to change denied/granted permission in Android M?

查看:121
本文介绍了如何更改Android M中的拒绝/授予权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何为用户提供在App中更改其权限的机会, 他已经设置为授予/拒绝了?

How can I give the user a chance to change his permissions in the App, which he has already set to granted/denied?

比方说,用户拒绝了权限.稍后,他想授予此权限. 是否有可能?我该怎么办?

Let's say a user denied a permission. Later he want's to grant this permission. Is it possible? How can I do that?

推荐答案

如果用户拒绝了权限,则可以检查该权限,如果他也没有选中从不显示",则可以打开自己的对话框,以解释您的应用需要该权限,然后从该对话框中,可以将用户带到应用设置,如果需要,他可以允许该权限.

In case user has denied the permission you can check for the permission and if he also has checked never show again, in that case you can open your own dialog explaining the need of that permission for your app and from that dialog you can take the user to the app settings where he can allow the permission if he wanted.

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 M中的拒绝/授予权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆