运行时权限:shouldshowrequestpermissionrationale始终返回false

时间:2017-08-15 00:16:38

标签: android runtime-permissions

下面是我的代码,我调用此代码获取运行时权限。在这种情况下,“shouldshowrequestpermissionrationale总是返回false”。我无法找到解决方案,为什么它像这样。因此,不显示运行时许可警报。建议我一个解决方案...

   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();
    }
}

2 个答案:

答案 0 :(得分:1)

谷歌文档中的

  

“如果应用先前已请求此权限且用户拒绝了该请求,则此方法返回true。”

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

最好的方法是始终在 onRequestPermissionsResult(...)

中使用 requestPermissions(...)

答案 1 :(得分:0)

您正在调用的方法是问问题"我们是否应该说明请求此权限的原因?"

来自文档"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块中只是简单地

ActivityCompat.requestPermissions(WelcomeActivity.this,permissionsRequired,PERMISSION_CALLBACK_CONSTANT);