权限管理器--分离出禁止与询问情况

自存 关于权限获取与回调


下面是权限请求的Util:

package fan.soundrecordingdemo.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by Dragon on 2017/11/8.
 */
public class PermissionsUtils {
    static final int VOICE_REQUEST_CODE = 66;

    private String[] permissions;
    private OnPermissionsCallback onPermissionsCallback;

    /**
     * 单例
     *
     * @param permissions           请求权限
     * @param onPermissionsCallback 回调
     * @return
     */
    public static PermissionsUtils permission(String[] permissions, OnPermissionsCallback onPermissionsCallback) {
        return new PermissionsUtils(permissions, onPermissionsCallback);
    }


    /**
     * 初始化
     *
     * @param onPermissionsCallback 回调
     */
    private PermissionsUtils(OnPermissionsCallback onPermissionsCallback) {
        this(null, onPermissionsCallback);
    }

    /**
     * 初始化
     *
     * @param permissions           请求权限
     * @param onPermissionsCallback 回调
     */
    private PermissionsUtils(String[] permissions, OnPermissionsCallback onPermissionsCallback) {
        this.onPermissionsCallback = onPermissionsCallback;
        this.permissions = permissions;
    }


    /**
     * 请求权限
     *
     * @return
     */
    public boolean requestPermissions(Activity activity) {
        return requestPermissions(activity, permissions);
    }


    /**
     * 请求权限
     *
     * @param activity    上下文
     * @param permissions 请求权限
     */
    private boolean requestPermissions(Activity activity, String[] permissions) {
        this.permissions = permissions;
        //判断是否开启摄像头权限
        if ((ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
                (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) {
            return true;
            //判断是否开启语音权限
        } else {
            //请求获取摄像头权限
//            Toast.makeText(mContext,"无权限",Toast.LENGTH_SHORT).show();
            if (permissions != null)
                ActivityCompat.requestPermissions(activity,
                        permissions, VOICE_REQUEST_CODE);
            return false;
        }
    }

    
    /**
     * 请求权限结果,对应onRequestPermissionsResult()方法。
     *
     * @param requestCode  请求返回码
     * @param permissions  请求权限
     * @param grantResults 请求状态
     */
    public void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions, int[] grantResults) {
        boolean isAllow = true;
//        String[] noAllowPermissions = new String[]{};
        List<String> noAllowPermissions = new ArrayList<>();

        int size = 0;
        if (requestCode == VOICE_REQUEST_CODE) {
            for (int i = 0; i < grantResults.length; i++) {
                if (!(grantResults[i] == PackageManager.PERMISSION_DENIED)) {
                    /**
                     * 给权限
                     */
                } else {
                    if (isAllow) {
                        isAllow = false;
                    }
                    /**
                     * 没给权限
                     */
                    //用户不同意,向用户展示该权限作用
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i])) {
                        /**
                         * 禁止询问
                         */
                        if (permissions.length > 0)
                            noAllowPermissions.add(permissions[i]);
                    } else {
                        /**
                         * 询问
                         */
                        onPermissionsCallback.onASK();
                    }
                }
                if (i == grantResults.length - 1) {
                    /**
                     * 给权限
                     */
                    if (isAllow) {
                        onPermissionsCallback.onAllow();
                    }

                    /**
                     * 有权限被禁止了
                     */
                    if (noAllowPermissions.size() > 0) {
                        onPermissionsCallback.onProhibited(noAllowPermissions);
                    }
                }
            }
        }
    }
} public interface OnPermissionsCallback { void onAllow(); void onProhibited(List<String> noAllowPermissions); void onASK(); }}

这里是初始化:

permissionsUtils = PermissionsUtils.permission(
        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
        new PermissionsUtils.OnPermissionsCallback() {
            @Override
            public void onAllow() {
                Toast.makeText(getApplication(), "3", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProhibited(List<String> noAllowPermissions) {
                Toast.makeText(getApplication(), "5", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onASK() {
                Toast.makeText(getApplication(), "4", Toast.LENGTH_SHORT).show();
            }
        });

在需要请求的地方贴上这段代码:

permissionsUtils.requestPermissions(activity)

当然,如果需要动态添加请求权限,工具里有提供方法。

萌新一个,希望各个大佬多指点,不足之处希望能得到各位的评论,我会改正。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值