Android动态权限获取及其工具类的实现与整合

 Android6.0以上,即API 23以上动态权限获取,这里以录音权限为例,其他权限亦可这样动态获取。

    public void getPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            int request = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);//RECORD_AUDIO为录音权限
            if (request != PackageManager.PERMISSION_GRANTED)//缺少权限,进行权限申请
            {
                 //其中参数16为请求码,其范围在[0,65535]
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 16);//RECORD_AUDIO为录音权限
                return;
            } else {
                //权限同意
            }

        } else { //低于23 不需要特殊处理
        }

    }

通过ActivityCompat.requestPermissions(activity,permissions,requestCode)第二个参数是一个String数组,第三个参数是请求码便于在onRequestPermissionsResult 方法中根据requestCode进行判断.。

 ActivityCompat.requestPermissions(this,//上下文
                        new String[]{Manifest.permission.RECORD_AUDIO},//权限
                        REQUEST_CODE);//请求码

请求权限后会回调onRequestPermissionsResult方法,在activity中重写

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

grantResults是int类型的数组每个值为PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED 分别对应permissions的每个请求.。

可整合为一个工具类

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;

public class DynamicPermissionUtils {

    //权限项数组
    public static final String[] PERMISSIONS = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.SYSTEM_ALERT_WINDOW,
            Settings.ACTION_MANAGE_OVERLAY_PERMISSION
    };


    private static int mRequestCode = -1;

    private static OnPermissionListener mOnPermissionListener;

    /**
     * 权限请求回调
     */
    public interface OnPermissionListener {

        //权限通过
        void onPermissionGranted();

        //权限拒绝
        void onPermissionDenied();

    }

    /**
     * 请求响应的权限
     * @param context     Activity
     * @param requestCode 请求码
     * @param permissions 请求权限
     * @param listener    权限请求监听
     */
    public static void requestPermissions(Activity context, String[] permissions, int requestCode, OnPermissionListener listener) {
        if (Build.VERSION.SDK_INT < 23) {
            return;
        }
        mOnPermissionListener = listener;
        List deniedPermissions = new ArrayList();
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(context, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                deniedPermissions.add(permissions[i]);//添加未授予的权限
            }
        }

        if (deniedPermissions.size() > 0) {
            mRequestCode = requestCode;
            //其中请求码范围在[0,65535]
            ActivityCompat.requestPermissions(context, deniedPermissions.toArray(new String[deniedPermissions.size()]), requestCode);
        } else {
            mOnPermissionListener.onPermissionGranted();
        }
    }

    /**
     * 验证所有权限是否都已经授权
     */
    private static boolean verifyPermissions(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) {//拒绝权限
                System.out.println("grantResult: "+grantResult);
                return false;
            }
        }
        return true;
    }

    /**
     * 请求权限结果,对应Activity中onRequestPermissionsResult()方法。
     */
    public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (mRequestCode != -1 && requestCode == mRequestCode) {
            if (mOnPermissionListener != null) {
                if (verifyPermissions(grantResults)) {
                    mOnPermissionListener.onPermissionGranted();
                } else {
                    mOnPermissionListener.onPermissionDenied();
                }
            }
        }
    }


}

使用在某个Activity中添加以下两个方法,并调用requestPermissions()方法即可

    //权限申请
    public void requestPermissions() {
        DynamicPermissionUtils.requestPermissions(this,  DynamicPermissionUtils.PERMISSIONS, 1,
                new DynamicPermissionUtils.OnPermissionListener() {//实现接口方法

            @Override
            public void onPermissionGranted() {//获取权限成功
                Toast.makeText(getApplicationContext(),"onPermissionGranted()",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPermissionDenied() {//获取权限失败
                Toast.makeText(getApplicationContext(),"onPermissionDenied()",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        DynamicPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

你可能感兴趣的:(Android,动态权限管理,Android,动态权限管理)