Easy-to-use dynamic permission management

https://github.com/yanzhenjie/AndPermission

Personal record for learning, if there is any infringement, please contact to delete


public static String mContactsPermissionName = "";
    public static void checkContactsPermission(Context context, Activity activity, String[] permission, String permissionName) {
        mContactsPermissionName = permissionName;
        AndPermission.with(context)
                .permission(
                        permission
                )
                .rationale(mRationale)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        // user agrees to permissions
                        Uri uri = Uri.parse("content://contacts/people");
                        Intent intent = new Intent(Intent.ACTION_PICK, uri);
                        activity.startActivityForResult(intent, 0);
                    }
                })

                .onDenied(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        // User denied permission
                        if (AndPermission.hasAlwaysDeniedPermission(context, permissions)) {
                            // A Dialog is used here to show that the application cannot continue to run without these permissions, and ask the user whether to authorize in the settings.
                            final SettingService settingService = AndPermission.permissionSetting(context);
                            MessageDialog messageDialog = new MessageDialog(context, R.style.MessageDialog, "Set permissions", "This function needs to be set" + mContactsPermissionName + "Permissions can be used normally, click OK to set") {
                                @Override
                                public void yesDialog() {
                                    // If the user agrees to set:
                                    settingService.execute();
                                }

                                @Override
                                public void cancelDialog() {
                                    // If the user does not agree to set:
                                    settingService.cancel();
                                }
                            };
                            messageDialog.show();

                        }
                    }
                })
                .start();
    }

    public static Rationale mRationale = new Rationale() {
        @Override
        public void showRationale(Context context, List<String> permissions,
                                  final RequestExecutor executor) {

// Here a Dialog is used to ask the user whether to continue authorization.
            MessageDialog messageDialog = new MessageDialog(context, R.style.MessageDialog, "Set permissions", "This function needs to be set" + mContactsPermissionName + "Permissions can be used normally, click OK to set") {
                @Override
                public void yesDialog() {
                    // If the user continues:
                    executor.execute();
                }

                @Override
                public void cancelDialog() {
                    // If user interrupts:
                    executor.cancel();
                }
            };
            messageDialog.show();

        }
    };

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325731935&siteId=291194637