Prithniraj Nicyone
Prithniraj Nicyone

Reputation: 5111

Marshmallow new permission model

I want to implement new marshmallow support in application by giving the permission model to my app. Now I have given the below permission in my manifest file.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Now in an activity I have loaded a fragment in which I am having an recyclerview and data is loaded in this recyclerview using recyclerview adapter.

In this recyclerview adapter's list, I ma having a button clicking on which I would require the above permission. So, I have called the below function when the user will click on this button.

    public void checkForManifestPermission(){

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);

        } else {

            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

Now in the activity I have overridden the below callback method (from http://web.archive.org/web/20160111030837/http://developer.android.com/training/permissions/requesting.html).

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;

            } else {

                ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false;
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

In the adapter where I am calling function checkForManifestPermission and checked the condition if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){} Then only redirect to other activity.

Now my problem is that the code written inside this condition get executed before calling the onRequestPermissionsResult method, I want that if permission granted by user then only it should redirect to other activity.

How can I achieve this, I want my code to wait for the user to give response about approve or deny the permission and then proceed further accordingly.

Thanks a lot for the help.

Upvotes: 1

Views: 821

Answers (3)

theblitz
theblitz

Reputation: 6881

I had similar problems in two different projects I worked on.

In one I simply performed the check of the permission on entry to the Activity. If no permission is given then I disabled the button.

If this is not practical then the other possibility is to take the code that, as you described, "is in your condition" and put it into a separate method. Then change checkForManifestPermission() to return a boolean as to whether the program already has the required permission or not.

So, that part of the code would read something like this:

private void methodThatNeedsPermission(){
   if (checkForManifestPermission())
       doTheAction();
}

 private void doTheAction(){
  ---- your code
}

Now, in the onRequestPermissionsResult, if the user okays the permission you can then call that same method. So, it becomes:

    @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
            ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;
            doTheAction(); 

        } else {

            ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false;
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'case' lines to check for other
    // permissions this app might request
}

Upvotes: 0

Bhumit
Bhumit

Reputation: 338

You can set below code for check permission in marshmallow :

if ((int) Build.VERSION.SDK_INT >= 23) {
                if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider WRITE_EXTERNAL_STORAGE
                    if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    } else {
                        ActivityCompat.requestPermissions(mActivity,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                    return;
                }
            }

Upvotes: 0

Amy
Amy

Reputation: 4032

Try This:

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

    if (requestCode == PERMISSIONS_CODE) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    onPPSButtonPress();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_CODE);
                }
            }
        }
    }
}

Note: Add your Read permission code as like above.

Upvotes: 0

Related Questions