标签:builder contact rmi exec man href 设置权限 create list
ZcxPremission为权限请求框架,基于aspectj实现,使用注解即可请求权限,简单方便。
NeedPermission支持在所有的方法中使用和activity的类上使用,提供了权限前操作,权限后的处理(权限拒绝或不再提醒)等。
在你的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
欢迎使用
标签:builder contact rmi exec man href 设置权限 create list
原文地址:https://www.cnblogs.com/xiaoXiangGuo/p/11684671.html