6.0动态权限管理

本文详细介绍了在Android 6.0及以上系统中如何进行动态权限管理,包括在Activity中添加回调处理权限请求的过程,帮助开发者理解和实现符合最新Android权限模型的应用程序。
摘要由CSDN通过智能技术生成
public class PermissionManager {
    public static final int PERMISSION_REQUEST = 100101;

    public interface PermissionResultCallBack {
        /**
         * 授权成功
         */
        void permissionSuccess();

        /**
         * 授权失败
         */
        void permissionFail();

        /**
         * 应用管理设置
         */
        void toSetPermission();

        /**
         * 标记不再提醒
         */
        void noTipPermission();
    }

    /**
     * 判断是否已经获取到权限
     *
     * @param permission
     * @return
     */
    public static boolean isHasPermission(String permission) {
        return ContextCompat.checkSelfPermission(ApplicationManager.getContext(), permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static void needPermission(final Activity context, final String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            // 第一次请求权限时,用户如果拒绝,下一次请求shouldShowRequestPermissionRationale()返回true
            // 向用户解释需要这个权限
            //if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE))
            ActivityCompat.requestPermissions(context,
                    new String[]{permission}, PERMISSION_REQUEST);
        }
    }

    public static void needPermission(final Activity context, final String[] permission) {
        if (permission.length != 2)
            throw new RuntimeException("同时申请权限个数固定为两个");
        if (ContextCompat.checkSelfPermission(context, permission[0]) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(context, permission[1]) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[]{permission[1]}, PERMISSION_REQUEST);
        } else if (ContextCompat.checkSelfPermission(context, permission[0]) != PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(context, permission[1]) == PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[]{permission[0]}, PERMISSION_REQUEST);
        } else if (ContextCompat.checkSelfPermission(context, permission[0]) != PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(context, permission[1]) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, permission, PERMISSION_REQUEST);
        }
    }

    public static void permissionResult(final Activity context, final String[] permissions, int[] grantResults, int requestCode, final PermissionResultCallBack resultCallBack) {
        if (requestCode != PERMISSION_REQUEST||grantResults.length == 0)
            return;
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            resultCallBack.permissionSuccess();
        } else {
            //用户勾选了不再询问
            //提示用户手动打开权限
            if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permissions[0])) {
                resultCallBack.noTipPermission();
                if (!TextUtils.equals(permissions[0], Manifest.permission.ACCESS_FINE_LOCATION)) {
                    DialogFactory dialogFactory = new DialogFactory(context, "权限设置", getTips(permissions[0]), false, new DialogCallBack() {
                        @Override
                        public void titleRightOnClickListener() {

                        }

                        @Override
                        public void positiveOnClickListener() {
                            resultCallBack.toSetPermission();
                            Intent intent = new Intent();
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
                            context.startActivity(intent);
                        }

                        @Override
                        public void negativeOnClickListener() {

                        }
                    });
                    dialogFactory.setCancel(false);
                    dialogFactory.setNamePositiveButton("去开启");
                    dialogFactory.show();
                }
            } else {
                resultCallBack.permissionFail();
            }
        }
    }

    private static String getTips(String permission) {
        String tips = "";
        switch (permission) {
            case Manifest.permission.READ_PHONE_STATE:
                tips = "开启获取手机信息权限后,大姨妈才能更好地为您服务哦~";
                break;
            case Manifest.permission.READ_EXTERNAL_STORAGE:
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                tips = "开启存储权限后,大姨妈才能更好地为您服务哦~";
                break;
            case Manifest.permission.RECORD_AUDIO:
                tips = "开启麦克风权限后,大姨妈才能更好地为您服务哦~";
                break;
            case Manifest.permission.ACCESS_FINE_LOCATION:
                tips = "开启定位权限后,大姨妈才能更好地为您服务哦~";
                break;
            case Manifest.permission.CAMERA:
                tips = "开启相机权限后,大姨妈才能更好地为您服务哦~";
                break;
        }
        return tips;
    }
}

Activity中添加回调

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.permissionResult(this, permissions, grantResults, requestCode, new PermissionManager.PermissionResultCallBack() {
            @Override
            public void permissionSuccess() {
            }

            @Override
            public void permissionFail() {
            }

            @Override
            public void toSetPermission() {
            }

            @Override
            public void noTipPermission() {
            }
        });
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值