简体   繁体   中英

My app Does not ask at the first time for the runtime permission for the marshmallow?

I making an app for the food delivery. In my app when the user use android phone version of marshmallow then my app does not ask at the first time for the permission of the read the sms. but when user open the app next time then my app ask for the permission. I don't know what is the problem. I want that when the user open the app first time permission should ask.instead of the second time.

This is my method for asking the permission I am calling this method in my onCreate() method of my Activity:

 private void permissionForMarshMallow() {

        int permissionCheck = ContextCompat.checkSelfPermission(LoginActivity.this,
                Manifest.permission.READ_SMS);

        if (ContextCompat.checkSelfPermission(LoginActivity.this,
                Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED) {


            if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this,
                    Manifest.permission.READ_SMS)) {

                Log.e("permission...granted", "permission granted............");


            } else {



                ActivityCompat.requestPermissions(LoginActivity.this,
                        new String[]{Manifest.permission.READ_SMS},
                        REQUEST_CODE_ASK_SINGLE_PERMISSION);


            }
        }
    } 

and this is the override method onRequestPermissionsResult() :

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {


        switch (requestCode) {

            case REQUEST_CODE_ASK_SINGLE_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {


                    // Toast.makeText(LoginActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }


    }

Can anyone tell me How can I make the app that it ask for the permission at the first time not second time?

There is a logic flaw in your code. This is how your code runs:

  1. Check if we have the permission.
  2. If we DO NOT have the permission:
    • Should we ask permission?
      • If yes, log "permission granted"
      • If not, then request permission

So basically, you are requesting permission when you shouldn't. In a case where you should ask for permission, instead of asking permission you just log "permission granted". You just have to change your code a little bit:

 private void permissionForMarshmallow() {

        int permissionCheck = ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_SMS);

        if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_SINGLE_PERMISSION);

        }
        else {
            // user probably checked "never ask again" - we should not ask for permission.
        }
    }
} 

Looks like at first start shouldShowRequestPermissionRationale is true and you just create a log entry. You wrote it in your log but in this case the permission is not granted yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM