AndroidBDMap学习02:获取用户的相关权限

封装:getPermissions()

实现:

private void getPermissions() {
        /**
         * 处理新版本API的代码
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList permissions = new ArrayList();
            /***
             * 定位权限为必须权限,用户如果禁止,则每次进入都会申请
             */
            // 定位精确位置
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            // 定位粗略位置
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }
            /*
             * 读写权限和电话状态权限非必要权限(建议授予)只会申请一次,用户同意或者禁止,只会弹一次
             */
            // 读写权限
            if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                //如果未获得权限,则准备批量申请
                permissionInfo += "Manifest.permission.WRITE_EXTERNAL_STORAGE Deny \n";
            }
            if (permissions.size() > 0) {
//批量申请 requestPermissions(permissions.toArray(
new String[permissions.size()]), SDK_PERMISSION_REQUEST); } } }

 子功能函数addPermission(ArrayList permissionsList, String permission)

实现:

private boolean addPermission(ArrayList permissionsList, String permission) {
        // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            if (shouldShowRequestPermissionRationale(permission)) {//是否弹出请求用户授权
                return true;
            } else {
                permissionsList.add(permission);
                return false;
            }
        } else {
            return true;
        }
    }

 

你可能感兴趣的:(AndroidBDMap学习02:获取用户的相关权限)