LostPuppy
LostPuppy

Reputation: 2501

Android Marshmallow permissions best practices

I have a pretty robust android application which does a lot of disc access in various activities and services and has no single point of access. Which was fine pre-marshmallow. Asking the permissions the first time and handling when the permissions being declined is fine but what if the user revokes the permissions after granting them. Now I am in a situation where I have 1000's of lines of code which needs to do a permission check in multiple places. So what is the best course of action

1) Multiple if checks sound too laborious.

2) Blanket Try Catch

Is there a better way to handle it?

Upvotes: 2

Views: 1471

Answers (3)

Harsh Patel
Harsh Patel

Reputation: 667

You have to check permission each and every time whenever you use. For your simplicity you can use PermissionAcceptor-master library.

Almost all permission is included in this library.

You have to add few lines of code using PermissionAcceptor-master library like

new PermissionRequest(MainActivity.this,
            Permission.PERMISSION_CAMERA,
            PermissionCode.CODE_PERMISSION_CAMERA,
            R.string.permission_camera_rationale,
            R.string.permission_camera_denied,
            R.string.permission_enable_message, this)
            .checkPermission();

For more information, visit PermissionAcceptor-master.

Upvotes: 1

igorepst
igorepst

Reputation: 1240

If the user revokes a permission after giving it, it's done via Settings, in which case your app is closed and restarted anyway. BaseActivity may indeed help to handle onRequestPermissionsResult

Upvotes: 0

Libin
Libin

Reputation: 17085

I had similar issue with my application, on multiple activities accessing an API which requires write storage permission. I end up having a BaseActivity class which checks for permission when activity resume and if permission is not granted it navigates the user to the initial activity. The Initial activity will never let the customer to other activities if permission is not granted.

Upvotes: 0

Related Questions