美文网首页
Permissions4m的使用

Permissions4m的使用

作者: 小和尚恋红尘 | 来源:发表于2018-02-28 17:30 被阅读0次

    permissions4m,是国产 Android 权限申请最佳适配方案。解决了在小米手机上进行权限申请时,所遇到的一些坑,也增强了魅族手机中的权限申请。
    下面我们直接看其使用:
    申请权限

            Permissions4M.get(this)
                         .requestForce(true)//是否强制弹出权限申请对话框
                         .requestPageType(Permissions4M.PageType.MANAGER_PAGE)//返回的 intent 是跳转至**系统设置页面**
                         .requestPermissions(Manifest.permission.ACCESS_COARSE_LOCATION)//申请的权限
                         .requestCodes(LOCATION_CODE)//申请的code(自定义)
                         .request();
    

    权限申请成功后回调

    /**
         * 授权成功是回调
         * @param code
         */
        @PermissionsGranted({LOCATION_CODE})
        public void granted(int code){
            switch (code){
                case LOCATION_CODE:
                    ToastUtils.showShort("定位权限申请成功");
                    break;
                default:
                    break;
            }
        }
    

    权限申请失败后回调

    /**
         * 授权失败时回调
         * @param code
         */
        @PermissionsDenied({LOCATION_CODE})
        public void denied(int code){
            switch (code){
                case LOCATION_CODE:
                    ToastUtils.showShort("定位权限申请失败");
                    break;
                default:
                    break;
            }
        }
    

    权限申请,二次授权时回调
    如果用户不需要自己弹出提示对话框的话,使用@PermissionsRationale,具体代码如下:

    /**
         * 二次授权时回调
         * @param code
         * 不需要自己弹出对话框的话,使用 PermissionsRationale
         */
        @PermissionsRationale({LOCATION_CODE})
        public void rationale(int code){
            switch (code){
                case LOCATION_CODE:
                    ToastUtils.showShort("需要定位权限,应用才能正常使用...");
                    break;
                default:
                    break;
            }
        }
    

    如果用户需要自己弹出提示对话框的话,使用@PermissionsCustomRationale,具体代码如下:

    /**
         * 二次授权时回调
         * @param code
         * 需要自己弹出对话框的话,使用 PermissionsCustomRationale
         */
        @PermissionsCustomRationale({LOCATION_CODE})
        public void locationRationale(int code){
            switch (code){
                case LOCATION_CODE:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("亲爱的用户,我们需要开启您的定位权限.")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ToastUtils.showShort("前往设置界面");
                                    Permissions4M.get(PermissionActivity.this)
                                            .requestOnRationale()
                                            .requestPermissions(Manifest.permission.ACCESS_COARSE_LOCATION)
                                            .requestCodes(LOCATION_CODE)
                                            .request();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                default:
                    break;
            }
        }
    

    如果用户拒绝权限申请且选择了不在提示时,使用@PermissionsNonRationale;具体代码如下:

    /**
         * 二次授权时回调
         * @param code
         * 拒绝权限且不再提示时,使用下面 PermissionsNonRationale
         */
        @PermissionsNonRationale({LOCATION_CODE})
        public void nonRationale(int code, final Intent intent){
            switch (code){
                case LOCATION_CODE:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("亲爱的用户,我们需要开启您的定位权限.")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ToastUtils.showShort("前往设置界面");
                                    startActivity(intent);//此处的Intent 类型为两种,一种是跳转至系统设置页面,另一种是跳至手机管家页面,而具体的设置方法请参考 注解回调 中 .requestPageType(int) 设置方法。
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                default:
                    break;
            }
        }
    

    当用户拒绝权限且选择不在提示时,会调用此权限申请回调方法

    /**
         * 在 Activity 或 Fragment 中,需要手动添加 onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)方法以支持权限申请回调
         * 拒绝权限且选择不在提示时,会调用此权限申请回调方法
         */
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            Permissions4M.onRequestPermissionsResult(PermissionActivity.this, requestCode, grantResults);
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    
    补充:一次性申请多种权限
    @PermissionsRequestSync(permission = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, value = {REQUEST_CAMERA, REQUEST_WRITE_STORAGE})
    
    public class MainActivity extends AppCompatActivity {
        private final String TAG = MainActivity.class.getSimpleName();
        public static final int REQUEST_CAMERA = 0x1000;
        public static final int REQUEST_WRITE_STORAGE = 0x1001;
    

    可以看到一次性申请多种权限是使用@PermissionsRequestSync,并且是写在类之外的;而且定义的code是公共的静态常量。这样要申请的权限和需要的code就定义完成了,下面看代码中的使用。

            Permissions4M.get(this)
                    .requestForce(true)//是否强制弹出权限申请对话框
                    .requestSync();
    

    对于申请成功和失败的处理和前面的差不多,看代码:

        @PermissionsGranted({REQUEST_CAMERA, REQUEST_WRITE_STORAGE})
        public void granted(int code) {
            switch (code) {
                case REQUEST_CAMERA:
                    Log.e(TAG, "照相机权限申请成功...");
                    break;
                case REQUEST_WRITE_STORAGE:
                    Log.e(TAG, "写存储权限申请成功...");
                    break;
                default:
                    break;
            }
        }
    
        @PermissionsDenied({REQUEST_CAMERA, REQUEST_WRITE_STORAGE})
        public void denied(int code) {
            switch (code) {
                case REQUEST_CAMERA:
                    Log.e(TAG, "照相机权限申请失败...");
                    break;
                case REQUEST_WRITE_STORAGE:
                    Log.e(TAG, "写存储权限申请失败...");
                    break;
                default:
                    break;
            }
        }
    

    一次性多种权限申请,也可以用下面的这个方法完成:

            Permissions4M.get(this)
                    .requestForce(true)
                    .requestPermissions(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .requestCodes(100, 101, 102)
                    .requestListener(new ListenerWrapper.PermissionRequestListener() {
                        @Override
                        public void permissionGranted(int i) {
                            switch (i){
                                case 100:
                                    Log.e("LHC", "照相机权限申请成功.");
                                    break;
                                case 101:
                                    Log.e("LHC", "手机信息权限申请成功.");
                                    break;
                                case 102:
                                    Log.e("LHC", "读权限申请成功.");
                                    break;
                                default:
                                    break;
                            }
                        }
    
                        @Override
                        public void permissionDenied(int i) {
                            switch (i){
                                case 100:
                                    Log.e("LHC", "照相机权限申请失败..");
                                    break;
                                case 101:
                                    Log.e("LHC", "手机信息权限申请失败..");
                                    break;
                                case 102:
                                    Log.e("LHC", "读写权限申请失败..");
                                    break;
                                default:
                                    break;
                            }
                        }
    
                        @Override
                        public void permissionRationale(int i) {//这个和permissionCustomRationale的调用二选一,根据自己的意愿。如果都写着也不影响,只会调用permissionCustomRationale。
                            switch (i){
                                case 100:
                                    Log.e("LHC", "请开启照相机权限");
                                    break;
                                case 101:
                                    Log.e("LHC", "请开启手机信息权限");
                                    break;
                                case 102:
                                    Log.e("LHC", "请开启手机读写权限");
                                    break;
                                default:
                                    break;
                            }
                        }
                    })
                    .requestCustomRationaleListener(new ListenerWrapper.PermissionCustomRationaleListener() {
                        @Override
                        public void permissionCustomRationale(int i) {
                            switch (i) {
                                case 100:
                                    new AlertDialog.Builder(PermissionActivity.this)
                                            .setMessage("亲爱的用户,我们需要开启您的照相机权限.")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    ToastUtils.showShort("前往设置界面");
                                                    Permissions4M.get(PermissionActivity.this)
                                                            .requestOnRationale()
                                                            .requestPermissions(Manifest.permission.CAMERA)
                                                            .requestCodes(100)
                                                            .request();
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            })
                                            .show();
                                    break;
                                case 101:
                                    new AlertDialog.Builder(PermissionActivity.this)
                                            .setMessage("亲爱的用户,我们需要开启您的手机信息权限.")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    ToastUtils.showShort("前往设置界面");
                                                    Permissions4M.get(PermissionActivity.this)
                                                            .requestOnRationale()
                                                            .requestPermissions(Manifest.permission.READ_PHONE_STATE)
                                                            .requestCodes(101)
                                                            .request();
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            })
                                            .show();
                                    break;
                                case 102:
                                    new AlertDialog.Builder(PermissionActivity.this)
                                            .setMessage("亲爱的用户,我们需要开启您的读写权限.")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    ToastUtils.showShort("前往设置界面");
                                                    Permissions4M.get(PermissionActivity.this)
                                                            .requestOnRationale()
                                                            .requestPermissions(Manifest.permission.READ_EXTERNAL_STORAGE)
                                                            .requestCodes(102)
                                                            .request();
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            })
                                            .show();
                                    break;
                                default:
                                    break;
                            }
                        }
                    })
                    .requestPage(new ListenerWrapper.PermissionPageListener() {
                        @Override
                        public void pageIntent(int i, final Intent intent) {
                            switch (i){
                                case 100:
                                    new AlertDialog.Builder(PermissionActivity.this)
                                            .setMessage("照相机权限申请:\n我们需要您开启读取照相机权限(in activity with listener)")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    startActivity(intent);
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.cancel();
                                                }
                                            })
                                            .show();
                                    break;
                                case 101:
                                    new AlertDialog.Builder(PermissionActivity.this)
                                            .setMessage("手机信息权限申请:\n我们需要您开启读取手机信息权限(in activity with listener)")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    startActivity(intent);
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.cancel();
                                                }
                                            })
                                            .show();
                                    break;
                                case 102:
                                    break;
                                default:
                                    break;
                            }
                        }
                    })
                    .request();
        }
    

    也可以将上面的实现写成:

            Permissions4M.get(this)
                    .requestForce(true)
                    .requestPermissions(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .requestCodes(100, 101, 102)
                    .requestListener(this)
                    .requestCustomRationaleListener(this)
                    .requestPage(this)
                    .request();
    
        @Override
        public void permissionGranted(int i) {
            switch (i) {
                case 100:
                    Log.e("LHC", "照相机权限申请成功.");
                    break;
                case 101:
                    Log.e("LHC", "手机信息权限申请成功.");
                    break;
                case 102:
                    Log.e("LHC", "读权限申请成功.");
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public void permissionDenied(int i) {
            switch (i) {
                case 100:
                    Log.e("LHC", "照相机权限申请失败..");
                    break;
                case 101:
                    Log.e("LHC", "手机信息权限申请失败..");
                    break;
                case 102:
                    Log.e("LHC", "读写权限申请失败..");
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public void permissionRationale(int i) {//这个和permissionCustomRationale的调用二选一,根据自己的意愿。如果都写着也不影响,只会调用permissionCustomRationale。
            switch (i) {
                case 100:
                    Log.e("LHC", "请开启照相机权限");
                    break;
                case 101:
                    Log.e("LHC", "请开启手机信息权限");
                    break;
                case 102:
                    Log.e("LHC", "请开启手机读写权限");
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public void permissionCustomRationale(int i) {
            switch (i) {
                case 100:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("亲爱的用户,我们需要开启您的照相机权限.")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ToastUtils.showShort("前往设置界面");
                                    Permissions4M.get(PermissionActivity.this)
                                            .requestOnRationale()
                                            .requestPermissions(Manifest.permission.CAMERA)
                                            .requestCodes(100)
                                            .request();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                case 101:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("亲爱的用户,我们需要开启您的手机信息权限.")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ToastUtils.showShort("前往设置界面");
                                    Permissions4M.get(PermissionActivity.this)
                                            .requestOnRationale()
                                            .requestPermissions(Manifest.permission.READ_PHONE_STATE)
                                            .requestCodes(101)
                                            .request();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                case 102:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("亲爱的用户,我们需要开启您的读写权限.")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ToastUtils.showShort("前往设置界面");
                                    Permissions4M.get(PermissionActivity.this)
                                            .requestOnRationale()
                                            .requestPermissions(Manifest.permission.READ_EXTERNAL_STORAGE)
                                            .requestCodes(102)
                                            .request();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .show();
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public void pageIntent(int i, final Intent intent) {
            switch (i) {
                case 100:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("照相机权限申请:\n我们需要您开启读取照相机权限(in activity with listener)")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(intent);
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            })
                            .show();
                    break;
                case 101:
                    new AlertDialog.Builder(PermissionActivity.this)
                            .setMessage("手机信息权限申请:\n我们需要您开启读取手机信息权限(in activity with listener)")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(intent);
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            })
                            .show();
                    break;
                case 102:
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    

    这样权限申请就算完成了.

    相关文章

      网友评论

          本文标题:Permissions4m的使用

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