android 中应用动态权限的封装

PermissionsUtils.java


/**
 * 应用动态权限设置工具类
 * Created by _psl on 2017/5/16.
 */
public class PermissionsUtils {

    /**
     * 相机需获取的权限
     */
    public static final String[] CAMERA_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    /**
     * 获取系统状态的权限
     */
    public static final String[] PHONE_STATE_PERMISSIONS = {Manifest.permission.READ_PHONE_STATE};
    /** 录音权限 */
    public static final String[] RECORD_AUDIO_PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
    /** 读写存储权限 */
    public static final String[] WRITE_EXTERNAL_STORAGE_PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    /** 定位权限*/
    public static final String[] LOCATION_PERMISSIONS = {Manifest.permission.ACCESS_FINE_LOCATION};

    /**
     * 检查是否需要动态获取权限
     *
     * @return true FALSE
     */
    public static boolean isRequestPermission() {
        // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

    // 开始提交请求权限
    public static boolean startRequestPermission(Activity activity, String[] permissions, int requestCode) {
        // 检查该权限是否已经获取
        int i = ContextCompat.checkSelfPermission(activity, permissions[0]);
        // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
        if (i != PackageManager.PERMISSION_GRANTED) {
            // 如果没有授予该权限,就去提示用户请求
            ActivityCompat.requestPermissions(activity, permissions, requestCode);
            return false;
        } else {
            return true;
        }
    }

    @TargetApi(23)
    public static boolean requestPermissionResult(Activity activity, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
            boolean result = activity.shouldShowRequestPermissionRationale(permissions[0]);
            if (!result) {
                // 用户还是想用我的 APP 的
                // 提示用户去应用设置界面手动开启权限
                return false;
            }
        } else {
            return true;
        }
        return false;
    }

}

使用方式

   if (PermissionsUtils.isRequestPermission()) {
                                if (PermissionsUtils.startRequestPermission(ProofSocialSecurityActivity.this, PermissionsUtils.WRITE_EXTERNAL_STORAGE_PERMISSIONS, 321)) {
                                    saveDownloadPictureAndJumpToActivity(bitmap,photoName);
                                }
                            } else {
                                saveDownloadPictureAndJumpToActivity(bitmap,photoName);
                            }
    // 用户权限 申请 的回调方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (PermissionsUtils.requestPermissionResult(this, permissions, grantResults)) {
            if (requestCode == 321) {
                saveDownloadPictureAndJumpToActivity(bitmap,photoName);

            }
        }
    }
    原文作者:追梦小乐
    原文地址: https://www.jianshu.com/p/f14b302305c9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞