aarav aarav - 2 months ago 6
Android Question

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

I making an app for the food delivery. In my app when the user use android phone version of marshmellow 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 permissionForMarshMellow() {

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?

Answer

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 permissionForMarshMellow() {

    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)) {

            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.
        }
    }
}