Runtime permission: shouldshowrequestpermissionrationale always returns false
Asked Answered
G

2

7

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();
    }
}
Giro answered 15/8, 2017 at 0:16 Comment(0)
E
7

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(...)

Engedi answered 27/11, 2017 at 2:15 Comment(0)
G
-1

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);
Galilee answered 15/8, 2017 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.