android基于AndPermission的权限申请封装类

依赖框架
项目地址:https://github.com/yanzhenjie/AndPermission
工具类:

package com.nbniu.app.common.tool;

import android.content.Context;
import android.os.Build;

import com.nbniu.app.common.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Setting;

import java.util.List;

/**
 * 文件描述 :
 * 创建人员 : 幽默GUY
 * 创建日期 : 2018/7/16 0016
 * 创建时间 : 0:57
 * 最后修改 :
 */
public class PermissionTool {

    public static void requestPermission(final Context context, final PermissionQuestListener listener, final String... permissionGroup) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (AndPermission.hasPermissions(context, permissionGroup)) {
            	if (listener != null)
                	listener.onGranted();
                return;
            }

            // 没有权限,申请权限
            AndPermission.with(context)
                    .runtime()
                    .permission(permissionGroup)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            if (listener != null)
                                listener.onGranted();
                        }
                    })
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(final List<String> data) {
                            if (AndPermission.hasAlwaysDeniedPermission(context, permissionGroup)) {
                            	//替换为自己的对话框
                                new QMUIDialog.MessageDialogBuilder(context)
                                        .setTitle("权限申请")
                                        .setMessage(listener.onAlwaysDeniedData())
                                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                                            @Override
                                            public void onClick(QMUIDialog dialog, int index) {
                                                dialog.dismiss();
                                                if (listener != null)
                                                    listener.onDenied(data);
                                            }
                                        })
                                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                                            @Override
                                            public void onClick(QMUIDialog dialog, int index) {
                                                dialog.dismiss();
                                                AndPermission.with(context)
                                                        .runtime()
                                                        .setting()
                                                        .onComeback(new Setting.Action() {
                                                            @Override
                                                            public void onAction() {
                                                                requestPermission(context, listener, permissionGroup);
                                                            }
                                                        })
                                                        .start();
                                            }
                                        })
                                        .setCancelable(false)
                                        .setCanceledOnTouchOutside(false)
                                        .create(R.style.DialogTheme2).show();
                            } else {
                                //拒绝后重复请求
                                requestPermission(context, listener, permissionGroup);
                            }
                        }
                    })
                    .start();
        } else {
        	if (listener != null)
            	listener.onGranted();
        }
    }

    /**
     * 权限申请监听
     */
    public interface PermissionQuestListener{
        void onGranted(); //允许获得权限后操作
        void onDenied(List<String> data); //拒绝权限后操作
        String onAlwaysDeniedData(); //拒绝后不再提示信息
    }
}

使用示例:

PermissionTool.requestPermission(LocationActivity.this, new PermissionTool.PermissionQuestListener() {
                    @Override
                    public void onGranted() {
                        mLocationClient.start();
                    }
                    @Override
                    public void onDenied(List<String> data) {
                       	Toast.makeText(context, "无法获取当前位置信息", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public String onAlwaysDeniedData() {
                        return "我们需要获得位置权限,是否前往设置?";
                    }
                }, Permission.Group.LOCATION);

你可能感兴趣的:(android)