请求授予权限时未显示requestpermission对话框 [英] requestpermission dialog is not showing up when requesting to grant permission

查看:848
本文介绍了请求授予权限时未显示requestpermission对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在请求 ACCESS_COARSE_LOCATION 权限,但是没有显示对话框.

I'm asking for ACCESS_COARSE_LOCATION permission but no dialog is showing up.

这是我的代码:

if (!checkPermissions()) {
    requestPermissions();
} else {
    getLastLocation();
}

这是 requestPermission():

private void requestPermissions() {
        boolean shouldProvideRationale =
                ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION);

        // Provide an additional rationale to the user. This would happen if the user denied the
        // request previously, but didn't check the "Don't ask again" checkbox.
        if (shouldProvideRationale) {
            Log.i(TAG, "Displaying permission rationale to provide additional context.");

            AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setMessage("Location permission is needed in order to show your current location");
            builder1.setCancelable(true);

            builder1.setPositiveButton(
                    "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO},
                                    PERMISSION_REQUEST_RECORD_AUDIO);
                        }
                    });

            AlertDialog alert11 = builder1.create();
            alert11.show();

        } else {
            Log.i(TAG, "Requesting permission");
            // Request permission. It's possible this can be auto answered if device policy
            // sets the permission in a given state or the user denied the permission
            // previously and checked "Never ask again".
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    REQUEST_PERMISSIONS_REQUEST_CODE);
        }
    }

我正在获取 08-31 13:02:25.067 8231-8231/com.appName.app/com.appName.app.MainActivity:正在请求权限,但没有对话框询问权限正在显示.

I'm getting 08-31 13:02:25.067 8231-8231/com.appName.app/com.appName.app.MainActivity: Requesting permission printed out but no dialog to ask permission is showing up.

为什么会这样?

推荐答案

对不起这个愚蠢的错误.

Sorry for the silly mistake.

我发现了问题所在.我忘记在 AndroidManifest.xml 中添加< uses-permission> 标记.

I figured out the problem. I forgot to add <uses-permission> tag in AndroidManifest.xml.

添加:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

< manifest> 标记下的 AndroidManifest.xml 中的

完成了该工作.

in AndroidManifest.xml under <manifest> tag did the job.

这篇关于请求授予权限时未显示requestpermission对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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