首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运行时权限: shouldshowrequestpermissionrationale总是返回false

运行时权限: shouldshowrequestpermissionrationale总是返回false
EN

Stack Overflow用户
提问于 2017-08-15 08:16:38
回答 2查看 2.9K关注 0票数 2

下面是我的代码,我调用此代码来获取运行时权限。在这种情况下,"shouldshowrequestpermissionrationale总是返回false“。我找不到一个解决方案,为什么它会变成这样。因此,不显示运行时权限警报。请给我一个解决方案。

代码语言:javascript
运行
复制
   private void checkRuntimePermission() {

    Logger.infoLog("checkRuntimePermission");
    if(ActivityCompat.checkSelfPermission(this, permissionsRequired[0]) != PackageManager.PERMISSION_GRANTED){
        Logger.infoLog("checkRuntimePermission first if");
        if(ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this,permissionsRequired[0])){
            Logger.infoLog("checkRuntimePermission if");
            //just request the permission
            //Show Information about why you need the permission
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Need Multiple Permissions");
            builder.setMessage("This app needs Camera and Location permissions.");
            builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        }else{
            Logger.infoLog("Show request permission rationale false");
        }
    } else {
        //You already have the permission, just go ahead.
        Logger.infoLog("Permission given already");
        proceedAfterPermission();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-11-27 10:15:39

在google文档中:

“如果应用程序以前请求过此权限,但用户拒绝了该请求,则此方法返回true。”

所以,你应该先调用requestPermissions(...),然后再使用shouldShowRequestPermissionRationale(...)可以得到你想要的结果。

最好的方法是在onRequestPermissionsResult(...)中始终使用requestPermissions(...)

票数 3
EN

Stack Overflow用户

发布于 2017-08-15 08:45:42

您正在调用的方法提出了这样的问题:“我们是否应该显示请求此权限的原因?”

从Docs "This method returns true if the app has requested this permission previously and the user denied the request." https://developer.android.com/training/permissions/requesting.html

如果该值为false,您仍然希望请求权限,但不需要显示警告对话框。因此,在else块中,只需简单地

代码语言:javascript
运行
复制
ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45684842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档