Viswa Sundhar
Viswa Sundhar

Reputation: 131

Runtime permission: shouldshowrequestpermissionrationale always returns false

Below is my code, i called this code for getting runtime permission. In this case the "shouldshowrequestpermissionrationale always returns false". I cannot find a solution why it is goind like this. Due to this the run time permission alert not displayed. Suggest me a solution pls...

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

Upvotes: 7

Views: 6457

Answers (2)

钟金宝
钟金宝

Reputation: 79

in google docs:

"This method returns true if the app has requested this permission previously and the user denied the request."

so, you should invoke requestPermissions(...) first, then use shouldShowRequestPermissionRationale(...) can get the result that you want.

The best method is that always using requestPermissions(...) in onRequestPermissionsResult(...)

Upvotes: 7

Matt Goodwin
Matt Goodwin

Reputation: 85

The method you are invoking is asking the question "should we show the reason for requesting this permission?"

From the 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

If that value is false, you still want to request the permission but you don't need to show the alert dialog. So, in the else block just simply

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

Upvotes: -1

Related Questions