简体   繁体   中英

Android is asking each permission three times and app closes after permissions are granted

I am creating a simple applications in which I need READ_CONTACT and CALL_PHONE permissions. I have written below code.

After installation app asks permissions 3 times like this -

1 of 2 read contacts
2 of 2 call and manage phone 


1 of 2 read contacts
2 of 2 call and manage phone 


1 of 2 read contacts
2 of 2 call and manage phone

Also After granting these permissions app does't open. But when I open app again, it works fine and does not ask permissions again.

I have following code

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_REQUESTS: {
            if (grantResults.length > 0) {
                boolean contactPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                boolean phonePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;

                if (contactPermission && phonePermission) {
                    // write your logic here
                } else {
                    Toast.makeText(this, "Read Contact & Call phone permissions are required", Toast.LENGTH_SHORT).show();
                    closeNow();
                }
            }
            break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            + ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale
                (this, Manifest.permission.READ_CONTACTS) ||
                ActivityCompat.shouldShowRequestPermissionRationale
                        (this, Manifest.permission.CALL_PHONE)) {

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE},
                    MULTIPLE_REQUESTS);
        }


    }

    setContentView(R.layout.activity_contact_app_bar);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(getTitle());

}

The permission should be asked when you invoke respective functionality .

I am suspecting the app is getting closed due to closeNow is called .

You need to debug below code .

if (grantResults.length > 0) { boolean contactPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED; boolean phonePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;

            if (contactPermission && phonePermission) {
                // write your logic here
            } else {
                Toast.makeText(this, "Read Contact & Call phone permissions are required", Toast.LENGTH_SHORT).show();
                closeNow();
            }
        }

try This Code

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
         ActivityCompat.requestPermissions(this,
            new String[]{ Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE},
            MULTIPLE_REQUESTS);
    }

add above code where you want permission

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_REQUESTS: {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] !=
                                PackageManager.PERMISSION_GRANTED) {
                     // Permission has been denied by user
                } else {
                    // Permission has been granted by user
                }
                return;
            }
        }
    }
}

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