Android:ActivityCompat.requestPermissionsdoesnotshowapopup(Android13,targetSdkVersion=33)
创始人
2024-10-06 03:42:19
0

在 Android 13 和 targetSdkVersion=33 上,Android 系统会对权限请求对话框进行更改,以便让用户更好地了解应用程序为何需要权限。由于此更改,请求权限时可能无法显示弹出窗口。解决此问题的方法是使用 shouldShowRequestPermissionRationale() 方法,以提示用户需要权限并显示对话框。

以下是一个使用 shouldShowRequestPermissionRationale() 方法解决此问题的示例代码:

private static final int PERMISSIONS_REQUEST_CODE = 100;

if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {

            // Show an explanation 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.
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Permission needed")
                    .setMessage("This permission is needed for accessing your files")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // continue with request
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                    PERMISSIONS_REQUEST_CODE);
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // close dialog
                            dialog.cancel();
                        }
                    })
                    .create()
                    .show();

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    PERMISSIONS_REQUEST_CODE);
        }
    }

在此示例中,如果我们的应用程序没有被授予读取外部存储权限,则会检查 shouldShowRequestPermissionRationale() 以查看是否需要显示有关权限请求的说明。如果需要,我们将显示 AlertDialog 来显示说明。如果用户

相关内容

热门资讯

两分钟辅助!哈糖大菠萝挂,德普... 两分钟辅助!哈糖大菠萝挂,德普之星辅助工具如何打开,资料教程(有挂教程)1、两分钟辅助!哈糖大菠萝挂...
三分钟辅助!hhpoker辅助... 三分钟辅助!hhpoker辅助靠谱吗,hh poker透视器下载,窍要教程(详细教程)所有人都在同一...
第九分钟辅助!德普之星透视,w... 第九分钟辅助!德普之星透视,wpk透视辅助方法,指引教程(有挂秘诀)进入游戏-大厅左侧-新手福利-激...
6分钟辅助!拱趴大菠萝机器人,... 6分钟辅助!拱趴大菠萝机器人,wepoker养号规律,绝活儿教程(有挂头条)1、拱趴大菠萝机器人有没...
第4分钟辅助!wepoker透... 第4分钟辅助!wepoker透视方法,德州私人局脚本,举措教程(有挂头条)德州私人局脚本能透视中分为...
9分钟辅助!德普之星私人局辅助... 9分钟辅助!德普之星私人局辅助器,xpoker辅助神器,机巧教程(证实有挂)进入游戏-大厅左侧-新手...
第3分钟辅助!we poker... 第3分钟辅助!we poker辅助器v3.3,wepoker透视功能下载,机巧教程(有挂透视)1、超...
第8分钟辅助!wepoker怎... 第8分钟辅助!wepoker怎么开辅助,黑侠破解wepoker,课程教程(今日头条)1、上手简单,内...
第十分钟辅助!如何下载wpk透... 第十分钟辅助!如何下载wpk透视版,pokerworld软件,策略教程(有挂分享)如何下载wpk透视...
九分钟辅助!wepoker辅助... 九分钟辅助!wepoker辅助真的假的,cloudpoker作弊,手筋教程(证实有挂)1、每一步都需...