Android动态权限申请工具类

package com.example.gaowen38.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.gaowen38.AppConstant;
import com.example.gaowen38.Base.BaseDialog;
import com.example.gaowen38.Base.MyBaseActivity;

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

import cn.view.viewlibrary.utils.Lg;
import cn.view.viewlibrary.utils.StringUtils;

public class PermissionUtils {
    public final static int REQUEST_CODE_ASK_PERMISSIONS = 54007;
    //默认运行权限
    private static String[] permissionArray = new String[]{
            Manifest.permission.WRITE_SETTINGS,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    //必须要开启的权限列表
    private static List<String> needRequestPermissionList = new ArrayList<>();


    /**
     * 判断所有权限是否已开启
     *
     * @param activity
     * @return
     */
    public static boolean isOpenAllPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getNeedPermission(activity);
            if (needRequestPermissionList != null && needRequestPermissionList.size() == 0) {
                return true;
            }
        } else {
            return true;
        }

        BaseDialog.showSimpleDialog(activity, "提醒", "请先开启权限", "确定", "取消", new BaseDialog.OnClickListener() {
            @Override
            public void onDialogClick(int buttonID) {
                if (buttonID == BaseDialog.ID_OK) {
                    startAppPermissionSettings(activity);
                }
            }
        });
        return false;
    }

    /**
     * 获取必须要注册的权限
     *
     * @param activity
     */
    private static void getNeedPermission(Activity activity) {
        if (needRequestPermissionList == null) {
            needRequestPermissionList = new ArrayList<>();
        }

        needRequestPermissionList.clear();

        for (int i = 0; i < permissionArray.length; i++) {
            if (ContextCompat.checkSelfPermission(activity, permissionArray[i]) != PackageManager.PERMISSION_GRANTED) {
                needRequestPermissionList.add(permissionArray[i]);
            }
        }
    }

    /**
     * 请求权限
     */
    public static void requestPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permissionArray.length; i++) {
                if (ContextCompat.checkSelfPermission(activity, permissionArray[i]) != PackageManager.PERMISSION_GRANTED) {
                    Lg.d(StringUtils.concat("当前开启权限:", permissionArray[i]));
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permissionArray[i])) {
                        //android 6.0权限 大于23时判断,其他的都是8.0权限,大于26判断
//                        if (permissionArray[i].equals(Manifest.permission.WRITE_SETTINGS) ) {
//                            if ( !Settings.System.canWrite(activity)) {
//                                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
//                                intent.setData(Uri.parse("package:" + DeviceUtils.getAppPackageName()));
//                                activity.startActivity(intent);
//                            }
//                        } else {
                            ActivityCompat.requestPermissions(activity, permissionArray, REQUEST_CODE_ASK_PERMISSIONS);
//                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 请求安装未知应用权限 (android 8.0新特性)
     */
    public static boolean requestInstallPermission(Activity activity) {
        boolean haveInstallPermission = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //先获取是否有安装未知来源应用的权限
            haveInstallPermission = activity.getPackageManager().canRequestPackageInstalls();
            if (!haveInstallPermission) {
                //没有权限
                Uri packageURI = Uri.parse("package:" + DeviceUtils.getAppPackageName());
                //注意这个是8.0新API
                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
                ((MyBaseActivity) activity).startActivityForResult(intent, AppConstant.m_permission_code_100);
            }
        }

        return haveInstallPermission;
    }

    /**
     * 启动当前应用的权限设置页面
     *
     * @param activity
     */
    private static void startAppPermissionSettings(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivity(intent);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值