【问题标题】:Runtime permission: shouldshowrequestpermissionrationale always returns false运行时权限:shouldshowrequestpermissionrationale 始终返回 false
【发布时间】:2017-08-15 00:16:37
【问题描述】:

以下是我的代码,我调用此代码以获得运行时权限。在这种情况下,“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();
    }
}

【问题讨论】:

    标签: android runtime-permissions


    【解决方案1】:

    在谷歌文档中:

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

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

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

    【讨论】:

      【解决方案2】:

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

      来自文档"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);
      

      【讨论】:

        猜你喜欢
        • 2018-11-05
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多