Android权限请求框架ZcxPremission

Android权限请求框架ZcxPremission,第1张

概述ZcxPremissionZcxPremission为权限请求框架,基于aspectj实现,使用注解即可请求权限,简单方便。NeedPermission支持在所有的方法中使用和activity的类上使用,提供了权限前 *** 作,权限后的处理(权限拒绝或不再提醒)等。使用到的类:ZcxPermission:用于初始化的类@NeedPermission:请 ZcxPremission

ZcxPremission为权限请求框架,基于aspectj实现,使用注解即可请求权限,简单方便。
NeedPermission支持在所有的方法中使用和activity的类上使用,提供了权限前 *** 作,权限后的处理(权限拒绝或不再提醒)等。

使用到的类:ZcxPermission:用于初始化的类@NeedPermission:请求权限的注解,可以作用于任何类的方法和activity类上,拥有参数:value:要请求的权限requestCode:请求码isAllowExecution:当权限被拒绝是否继续执行requestBefore:请求前调用的方法,和@PermissionBefore配合使用,会调用带有相同参数的被PermissionBefore注解的方法permissionCanceled:请求被取消的方法,和@PermissionCanceled配合使用,会调用带有相同参数的被PermissionCanceled注解的方法permissionDenIEd:请求被拒绝的方法,和@permissionDenIEd配合使用,会调用带有相同参数的被permissionDenIEd注解的方法@PermissionBefore:请求权限前的 *** 作,可以在请求权限的本类中和配置类中使用,被注解的方法的参数只能是PermissionBeforeBean
,与NeedPermission联合使用,通过requestBefore匹配@PermissionCanceled:权限被取消(用户点击禁止权限)时调用的方法,可以在请求权限的本类中和配置类中使用
,被注解的方法的参数只能是PermissionCanceledBean,与Neddpermission联合使用,通过permissionCanceled匹配@PermissionDenIEd:权限被取消(用户勾选禁止后不再提示并点击禁止权限)时调用的方法,可以在请求权限的本类中和配置类中使用
,被注解的方法的参数只能是PermissionDenIEdBean,与Neddpermission联合使用,通过permissionDenIEd匹配使用方式

在你的Application的onCreate方法中使用ZcxPermission.getInstance().init()初始化,然后在需要权限的方法
ZcxPermissionConfig该类用于配置统一的权限前置 *** 作、取消 *** 作、拒绝 *** 作的公共方法。

  public voID onCreate() {        super.onCreate();        ZcxPermission.getInstance().init(getApplicationContext(),new ZcxPermissionConfig());//ZcxPermissionConfig如果不用可设置为null}

方法中使用注解时:

@NeedPermission(value = {Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},            requestBefore = Manifest.permission.CAMERA,permissionCanceled = Manifest.permission.CAMERA,            permissionDenIEd = Manifest.permission.CAMERA,isAllowExecution = true)    private voID onClickContacts() {        FragmentManager fragmentManager = getFragmentManager();        Fragment cameraFragment = fragmentManager.findFragmentByTag("camera");        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        if (cameraFragment ==null) {            fragmentTransaction.replace(R.ID.sample_content_fragment, CameraPrevIEwFragment.newInstance(), "camera")                    .addToBackStack("camera")                    .commitAllowingStateLoss();        }else {            fragmentTransaction.show(cameraFragment);        }    }

activity中使用注解时:

@NeedPermission(value = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},        requestBefore = Manifest.permission.CAMERA, permissionCanceled = Manifest.permission.CAMERA,        permissionDenIEd = Manifest.permission.CAMERA, isAllowExecution = true)public class CameraActivity extends Activity implements PermissionListener {    @OverrIDe    public voID onPermissionGranted() {        mHasPermission =true;        initCamera();    }    @OverrIDe    public voID onPermissionCanceled(PermissionCanceledBean bean) {    }    @OverrIDe    public voID onPermissionDenIEd(PermissionDenIEdBean bean) {    }    @PermissionBefore    public voID before(final PermissionBeforeBean beforeBean){        new androID.support.v7.app.AlertDialog.Builder(this)                .setTitle("我们需要相机权限来正常拍照")                .setPositivebutton("确定", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        beforeBean.proceed(true);                    }                })                .setNegativebutton("取消", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        beforeBean.proceed(false);                    }                })                .show();    }}

ZcxPermissionConfig使用如下:

public    class ZcxPermissionConfig   {    @PermissionBefore(Manifest.permission.CAMERA)//该注解注解的方法参数只能是PermissionBeforeBean    public voID before(final PermissionBeforeBean beforeBean){        new androID.support.v7.app.AlertDialog.Builder(beforeBean.getContext())                .setTitle("来自配置文件,我们需要相机权限来正常拍照")                .setPositivebutton("确定", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        beforeBean.proceed(true);//必须调用这个方法,确保正常往下执行                    }                })                .setNegativebutton("取消", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        beforeBean.proceed(false);//必须调用这个方法,确保正常往下执行                    }                })                .show();    }    @PermissionCanceled(Manifest.permission.CAMERA)//该注解注解的方法参数只能是PermissionCanceledBean    public voID cancel(final PermissionCanceledBean canceledBean){        new androID.support.v7.app.AlertDialog.Builder(canceledBean.getContext())                .setTitle("来自配置文件,我们需要相机权限,请同意")                .setPositivebutton("确定", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        canceledBean.againRequest();//调用该方法重试请求                    }                })                .setNegativebutton("取消", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                    }                })                .show();    }    @PermissionDenIEd(Manifest.permission.CAMERA)//该注解注解的方法参数只能是PermissionDenIEdBean    public voID denIEd(final PermissionDenIEdBean denIEdBean){        new androID.support.v7.app.AlertDialog.Builder(denIEdBean.getContext())                .setTitle("来自配置文件,我们需要权限,是否设置")                .setPositivebutton("确定", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        denIEdBean.toSettingActivity();//打开设置界面设置权限                    }                })                .setNegativebutton("取消", new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                    }                })                .show();    }}

当然,如果你还有什么特殊需要也可以直接用工具类PermissionUtils

PermissionUtils.requestPermissions(context, permissions, requestCode, new PermissionListener() {            @OverrIDe            public voID onPermissionGranted() {                           }            @OverrIDe            public voID onPermissionCanceled(PermissionCanceledBean bean) {                            }            @OverrIDe            public voID onPermissionDenIEd(PermissionDenIEdBean bean) {                            }        });
依赖

在你的根目录的build.gradle中添加配置com.github.xiaoXiangGuo:ZcxAspectj:1.0.1如下:

buildscript {     repositorIEs {        maven { url "https://jitpack.io" }    }    dependencIEs {        classpath 'com.androID.tools.build:gradle:3.0.1'        classpath 'com.github.xiaoXiangGuo:ZcxAspectj:1.0.1'    }}allprojects {    repositorIEs {        maven { url "https://jitpack.io" }    }}

然后在你的app的build.gradle中使用插件和依赖

apply plugin: 'com.androID.application'apply plugin: 'zcx-aspectj-plugin'dependencIEs {    implementation 'com.github.xiaoXiangGuo:ZcxPremission:1.0.1'}

项目地址:https://github.com/xiaoXiangGuo/ZcxPremission
欢迎使用

总结

以上是内存溢出为你收集整理的Android权限请求框架ZcxPremission全部内容,希望文章能够帮你解决Android权限请求框架ZcxPremission所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1092849.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存