Chris Palma
Chris Palma

Reputation: 223

Denying Runtime Permission in Android Marshmallow Crashes App

I have trouble with handling runtime permissions in Android Marshmallow and I really need a hand. The problem is when I deny a runtime permission (when the dialog comes up), the app crashes. In more detail, the activity I am currently running is restarted (I just call it an app crash?).

Here is the code:

public void showContacts() {

    if (CommonData.showLogs) {
        Log.d(debugTag, "Show contacts button pressed. Checking permissions.");
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        requestContactsPermissions();
    } else {

        if (CommonData.showLogs) {
            Log.d(debugTag, "Contact permissions have already been granted. Displaying contact details.");
        }

        // Do my Stuff
    }
}

private void requestContactsPermissions() {

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

        Snackbar.make(rLParent, R.string.permission_contacts_rationale,
                Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        ActivityCompat
                                .requestPermissions(ImportContactsActivity.this, PERMISSIONS_CONTACT,
                                        REQUEST_CONTACTS);
                    }
                })
                .show();
    } else {

        ActivityCompat.requestPermissions(ImportContactsActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
    }
}

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


    if (requestCode == REQUEST_CONTACTS) {

        if (PermissionUtil.verifyPermissions(grantResults)) {

            // Do my Stuff
        } else {



            Snackbar.make(rLParent, R.string.permissions_not_granted,
                    Snackbar.LENGTH_SHORT)
                    .show();

        }

    } else {
        Log.d(debugTag, "Contacts permissions were NOT granted.");
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

What am I doing wrong here? Any help would be greatly appreciated. Thank you very much!

Upvotes: 0

Views: 1390

Answers (1)

Kushan
Kushan

Reputation: 5984

Try putting the actions performed using the Permission inside a Try-catch block and display a message or something when a SecurityException arises.

Eg... I used the following for Location Permissions after asking for the permissions...

Errortext is a textview displayed in the center of screen to show what the user needs to do...

            try {

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


            }catch (SecurityException se){
                Log.d("FragmentCreate","You don't have permissions");

                errortext.setVisibility(View.VISIBLE);
                errortext.setText("Please provide Location permission to continue, Settings->Apps->RecommendedApp->Permissions");
                Toast.makeText(this,"Please provide location permissions to continue",Toast.LENGTH_SHORT).show();
            }

Alternatively:

you can also show a dialogfragment inside the catch block with a button to take the user to the settings screen of your app.You can do a startActivityForResult() and wait for the returned value and check if the permission now exists

Upvotes: 1

Related Questions