安卓权限工具类

作者: 大笨龙___ | 来源:发表于2018-04-21 16:51 被阅读225次

    安卓6.0后敏感权限需要动态申请,近期对之前的权限工具类做了整理和改进。目标,让敲代码更加可控的ctrl c.
    此工具类支持activity 和fragment 当然fragment 的context 需要通过getConext 或者在onAttach 中拿activity 中的上下文

    下面是工具类操作的基本流程


    代码部分

    一、使用(注意 fragment 中需要填入activity 的上下文)

    PermissionManager.getInstance().getCameraPermission(getContext(), new PermissionManager.RequestPermission() {
                       @Override
                       public void isOk() {
                           Log.e("isOK","++++++++++++++++++++++++++++++++++++");
                       }
                       @Override
                       public void never() {
                           Log.e("never","++++++++++++++++++++++++++++++++++++");
                       }
                       @Override
                       public void refuse() {
                           Log.e("refuse","++++++++++++++++++++++++++++++++++++");
                       }
                   });
    

    在BaseActivity 中加入如下代码

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            PermissionManager.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        }
    

    二、管理类

    public class PermissionManager {
        private String storage=Manifest.permission.WRITE_EXTERNAL_STORAGE;
        private String camera =Manifest.permission.CAMERA;
    
        private final int requestcode_camera=1;
        private static PermissionManager permissionManager;
        private RequestPermission requestPermission;
        public static PermissionManager getInstance(){
            if(null==permissionManager){
                permissionManager=new PermissionManager();
            }
            return permissionManager;
        }
    
        //照相机和文件权限
        public void getCameraPermission(Context context,RequestPermission requestPermission){
            this.requestPermission=requestPermission;
            PermissionUtil.getInstance().checkPermission(context,new String[]{storage,camera},requestcode_camera);
        }
    
    
    
        /**
         * 一个或多个权限请求结果回调
         *
         * @param requestCode
         * @param permissions
         * @param grantResults
         */
        public void onRequestPermissionsResult(Activity context,int requestCode, String[] permissions, int[] grantResults) {
            if(requestPermission==null){
                return;
            }
            boolean hasAllGranted = true;
            for (int i = 0; i < grantResults.length; ++i) {
                if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    hasAllGranted = false;
                    // 可以推断出用户选择了“不在提示”选项,在这种情况下需要引导用户至设置页手动授权
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permissions[i])) {
                        requestPermission.never();
                    } else {
                        //权限请求失败,但未选中“不再提示”选项
                        requestPermission.refuse();
                    }
                    break;
                }
            }
            if (hasAllGranted) {
                //权限请求成功
                requestPermission.isOk();
            }
        }
    
        public interface  RequestPermission{
            public void isOk();
            public void never ();
            public void refuse();
        }
    }
    
    
    

    三、工具类

    
    /**
     * Created by biyunlong on 2017/7/7.
     * Make progress every day
     */
    
    public class PermissionUtil {
        public static PermissionUtil permissionUtil;
    
        public static PermissionUtil getInstance() {
            if (permissionUtil == null) {
                permissionUtil = new PermissionUtil();
            }
            return permissionUtil;
        }
    
        public boolean checkPermission(Context context, String[] permissions, int code) {
            int length = permissions.length;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ((Activity) context).requestPermissions(permissions, code);
            }
    
            for (int i = 0; i < length; i++) {
                if (ContextCompat.checkSelfPermission(context, permissions[i]) == PackageManager.PERMISSION_DENIED) {
                    return false;
                }
            }
            return true;
        }
    
        public boolean checkPermission(Context context, String permission, int code) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ((Activity) context).requestPermissions(new String[]{permission}, code);
            }
            if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) {
                return true;
            } else {
                return false;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:安卓权限工具类

        本文链接:https://www.haomeiwen.com/subject/tvgxlftx.html