一文彻底解决Android动态权限申请(下)

下面是完整版代码MyPermissionsUtil,MainActivity:

MyPermissionsUtil:

package top.lyoun.myusb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;

/**
 * 我自己的动态权限组,用于android6.0以上,并适配android8.0以上动态权限申请
 */
public class MyPermissionsUtil {
    //以下都是需要动态申请的权限组

    //STORAGE 存储卡文件读写权限组
    public final static int requestCode_storage = 100;
    public static String permissions_storage[] = new String[]{
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.READ_EXTERNAL_STORAGE"
    };

    //MICROPHONE 麦克风权限组
    public final static int requestCode_audio = 101;
    public static String permissions_audio[] = new String[]{
            "android.permission.RECORD_AUDIO"
    };


    //CAMERA 相机权限组
    public final static int requestCode_camera = 102;
    public static String permissions_camera[] = new String[]{
            "android.permission.CAMERA"
    };

    //LOCATION 位置权限组
    public final static int requestCode_location = 103;
    public static String permissions_location[] = new String[]{
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.ACCESS_COASE_LOCATION"
    };

    //PHONE 手机权限组
    public final static int requestCode_phone = 104;
    public static String permissions_phone[] = new String[]{
            "android.permission.READ_PHONE_STATE",
            "android.permission.CALL_PHONE",
            "android.permission.READ_CALL_LOG",
            "android.permission.WRITE_CALL_LOG"
    };

    //CALENDAR 日历权限组
    public final static int requestCode_calendar = 105;
    public static String permissions_calendar[] = new String[]{
            "android.permission.READ_CALENDAR",
            "android.permission.WRITE_CALENDAR"
    };

    //CONTACTS 联系人权限组
    public final static int requestCode_contacts = 106;
    public static String permissions_contacts[] = new String[]{
            "android.permission.READ_CONTACTS",
            "android.permission.WRITE_CONTACTS",
            "android.permission.GET_ACCOUNTS"
    };

    //SMS 短信权限组
    public final static int requestCode_sms = 107;
    public static String permissions_sms[] = new String[]{
            "android.permission.SEND_SMS",
            "android.permission.RECEIVE_SMS",
            "android.permission.READ_SMS",
            "android.permission.RECEIVE_WAP_PUSH"
    };

    //BODY_SENSORS 传感器权限组
    public final static int requestCode_sensors = 108;
    public static String permissions_sensors[] = new String[]{
            "android.permission.BODY_SENSORS"
    };

    public final static int requestCode_all = 109;
    public static int granted = PackageManager.PERMISSION_GRANTED;


    private Activity mActivity;
    private int mRequestCode;


    //mustGrantedPermiss,必须要授予的权限,如果没有授予这些权限,则提示用户必须要授权后,app才能正常用
    public static String[] mustGrantedPermiss = Splicing(permissions_storage, permissions_audio
            , permissions_camera,
            permissions_location,
            permissions_phone,
            permissions_calendar,
            permissions_contacts,
            permissions_sms,
            permissions_sensors
            );

    public void checkPermissions(Activity activity, String[] needChecks, int requestCode) {
        this.mActivity = activity;
        this.mRequestCode = requestCode;

        if (needChecks != null && needChecks.length > 0) {
            //权限检查,只有android6.0及其以上才需要动态权限检查
            if (android.os.Build.VERSION.SDK_INT > 22) {
                for (int i = 0; i < needChecks.length; i++) {
                    if (mActivity.checkSelfPermission(needChecks[i]) != granted) {
                        System.out.println("您缺少" + needChecks[i] + "权限,需要动态添加权限");
                        //requestCode 请求码,表明当前是对哪个权限的请求
                        ActivityCompat.requestPermissions(mActivity, needChecks, requestCode);
                        break;
                    }
                }
            }
        }
    }

    boolean storagesGranted = false; //表明该权限是否已被授权
    boolean audiosGranted = false;
    boolean camerasGranted = false;
    boolean locationsGranted = false;
    boolean phonesGranted = false;
    boolean calendarsGranted = false;
    boolean contactsGranted = false;
    boolean smsGranted = false;
    boolean sensorsGranted = false;

    String[] checkStorages = null; //表明客户端有没有请求授予该权限
    String[] checkAudios = null;
    String[] checkCameras = null;
    String[] checkLocations = null;
    String[] checkPhones = null;
    String[] checkCalendars = null;
    String[] checkContacts = null;
    String[] checkSms = null;
    String[] checkSensors = null;

    int storagesDenyed = 0; //表明当前权限有没有被用户拒绝
    int audiosDenyed = 0;
    int camerasDenyed = 0;
    int locationsDenyed = 0;
    int phonesDenyed = 0;
    int calendarsDenyed = 0;
    int contactsDenyed = 0;
    int smsDenyed = 0;
    int sensorsDenyed = 0;

    public static int compareCount = 0;
    public static int needCompareCount = 0;

    /**
     * @param activity
     * @param checkStorages  //存储卡文件读写权限组
     * @param checkAudios    //麦克风录音权限
     * @param checkCameras   //相机权限组
     * @param checkLocations //位置权限组
     * @param checkPhones    //手机权限组
     * @param checkCalendars //日历权限组
     * @param checkContacts  //联系人权限组
     * @param checkSms       //短信权限组
     * @param checkSensors   //传感器权限组
     */
    public void checkMorePermissions(Activity activity,
                                     String[] checkStorages,
                                     String[] checkAudios,
                                     String[] checkCameras,
                                     String[] checkLocations,
                                     String[] checkPhones,
                                     String[] checkCalendars,
                                     String[] checkContacts,
                                     String[] checkSms,
                                     String[] checkSensors) {
        this.mActivity = activity;
        this.checkStorages = checkStorages;
        this.checkAudios = checkAudios;
        this.checkCameras = checkCameras;
        this.checkLocations = checkLocations;
        this.checkPhones = checkPhones;
        this.checkCalendars = checkCalendars;
        this.checkContacts = checkContacts;
        this.checkSms = checkSms;
        this.checkSensors = checkSensors;

        //把数据初始化
        storagesDenyed = 0;
        audiosDenyed = 0;
        camerasDenyed = 0;
        locationsDenyed = 0;
        phonesDenyed = 0;
        calendarsDenyed = 0;
        contactsDenyed = 0;
        smsDenyed = 0;
        sensorsDenyed = 0;

        compareCount = 0;
        needCompareCount = 0;

        if(checkStorages != null && checkStorages.length > 0){
            needCompareCount++;
        }
        if(checkAudios != null && checkAudios.length > 0){
            needCompareCount++;
        }
        if(checkCameras != null && checkCameras.length > 0){
            needCompareCount++;
        }
        if(checkLocations != null && checkLocations.length > 0){
            needCompareCount++;
        }
        if(checkPhones != null && checkPhones.length > 0){
            needCompareCount++;
        }
        if(checkCalendars != null && checkCalendars.length > 0){
            needCompareCount++;
        }
        if(checkContacts != null && checkContacts.length > 0){
            needCompareCount++;
        }
        if(checkSms != null && checkSms.length > 0){
            needCompareCount++;
        }
        if(checkSensors != null && checkSensors.length > 0){
            needCompareCount++;
        }

        //权限检查,只有android6.0及其以上才需要动态权限检查
        if (android.os.Build.VERSION.SDK_INT > 22) {

            try {
                if (checkStorages != null && checkStorages.length > 0) {
                    int i = 0;
                    for (; i < checkStorages.length; i++) {
                        if (mActivity.checkSelfPermission(checkStorages[i]) != granted) {
                            System.out.println("您缺少" + checkStorages[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            storagesGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkStorages, requestCode_storage);
                            break;
                        }
                    }
                    if (i == checkStorages.length) {
                        storagesGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }

                if (checkAudios != null && checkAudios.length > 0 && checkStorages != null
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))) {//
                    int i = 0;
                    for (; i < checkAudios.length; i++) {
                        if (mActivity.checkSelfPermission(checkAudios[i]) != granted) {
                            System.out.println("您缺少" + checkAudios[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            audiosGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkAudios, requestCode_audio);

                            break;
                        }
                    }
                    if (i == checkAudios.length) {
                        audiosGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkCameras != null && checkCameras.length > 0
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))) {
                    int i = 0;
                    for (; i < checkCameras.length; i++) {
                        if (mActivity.checkSelfPermission(checkCameras[i]) != granted) {
                            System.out.println("您缺少" + checkCameras[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            camerasGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkCameras, requestCode_camera);
                            break;
                        }
                    }
                    if (i == checkCameras.length) {
                        camerasGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkLocations != null && checkLocations.length > 0 && !locationsGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))) {
                    int i = 0;
                    for (; i < checkLocations.length; i++) {
                        System.out.println("location: " + i + " : "
                        + checkLocations[i] + " ," + mActivity.checkSelfPermission(checkLocations[i]));
                        if (mActivity.checkSelfPermission(checkLocations[i]) == granted) {
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            locationsGranted = true;
                            if(needCompareCount > 0){
                                needCompareCount--;
                            }
                            break;
                        }
                    }

                    if (i == checkLocations.length && !locationsGranted) {
                        System.out.println("why location: " + i
                                + " ," + checkLocations.length + " ," + locationsGranted);
                        locationsGranted = false;
                        //needCompareCount++;
                        ActivityCompat.requestPermissions(mActivity, checkLocations, requestCode_location);
                    }
                }
                if (checkPhones != null && checkPhones.length > 0 && !phonesGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))
                        && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))) {//
                    int i = 0;
                    for (; i < checkPhones.length; i++) {
                        if (mActivity.checkSelfPermission(checkPhones[i]) != granted) {
                            System.out.println("您缺少" + checkPhones[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            phonesGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkPhones, requestCode_phone);
                            break;
                        }
                    }
                    if (i == checkPhones.length) {
                        phonesGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkCalendars != null && checkCalendars.length > 0 && !calendarsGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))
                        && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))
                        && (phonesGranted || !(checkPhones != null && checkPhones.length > 0))) {//&& (phonesGranted || !(checkPhones != null && checkPhones.length > 0))
                    int i = 0;
                    for (; i < checkCalendars.length; i++) {
                        if (mActivity.checkSelfPermission(checkCalendars[i]) != granted) {
                            System.out.println("您缺少" + checkCalendars[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            calendarsGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkCalendars, requestCode_calendar);
                            break;
                        }
                    }
                    if (i == checkCalendars.length) {
                        calendarsGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkContacts != null && checkContacts.length > 0 && !contactsGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))
                        && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))
                        && (phonesGranted || !(checkPhones != null && checkPhones.length > 0))
                        && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0))) {//
                    int i = 0;
                    for (; i < checkContacts.length; i++) {
                        if (mActivity.checkSelfPermission(checkContacts[i]) != granted) {
                            System.out.println("您缺少" + checkContacts[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            contactsGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkContacts, requestCode_contacts);
                            break;
                        }
                    }
                    if (i == checkContacts.length) {
                        contactsGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkSms != null && checkSms.length > 0 && !smsGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))
                        && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))
                        && (phonesGranted || !(checkPhones != null && checkPhones.length > 0))
                        && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0))
                        && (contactsGranted || !(checkContacts != null && checkContacts.length > 0))) {//
                    int i = 0;
                    for (; i < checkSms.length; i++) {
                        if (mActivity.checkSelfPermission(checkSms[i]) != granted) {
                            System.out.println("您缺少" + checkSms[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            smsGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkSms, requestCode_sms);
                            break;
                        }
                    }
                    if (i == checkSms.length) {
                        smsGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
                if (checkSensors != null && checkSensors.length > 0 && !sensorsGranted
                        && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))
                        && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))
                        && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))
                        && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))
                        && (phonesGranted || !(checkPhones != null && checkPhones.length > 0))
                        && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0))
                        && (contactsGranted || !(checkContacts != null && checkContacts.length > 0))
                        && (smsGranted || !(checkSms != null && checkSms.length > 0))) {//
                    int i = 0;
                    for (; i < checkSensors.length; i++) {
                        if (mActivity.checkSelfPermission(checkSensors[i]) != granted) {
                            System.out.println("您缺少" + checkSensors[i] + "权限,需要动态添加权限");
                            //requestCode 请求码,表明当前是对哪个权限的请求
                            sensorsGranted = false;
                            //needCompareCount++;
                            ActivityCompat.requestPermissions(mActivity, checkSensors, requestCode_sensors);
                            break;
                        }
                    }
                    if (i == checkSensors.length) {
                        sensorsGranted = true;
                        if(needCompareCount > 0){
                            needCompareCount--;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 不再提示权限时的展示对话框
     */
    AlertDialog mPermissionDialog;

    public void showSystemPermissionsSettingDialog(String tips) {
        final String mPackName = mActivity.getPackageName();
        if (mPermissionDialog == null) {
            mPermissionDialog = new AlertDialog.Builder(mActivity)
                    .setMessage(tips).setCancelable(false)
                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            cancelPermissionDialog();
                            Uri packageURI = Uri.parse("package:" + mPackName);
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            mActivity.startActivity(intent);
                            mActivity.finish();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //关闭页面或者做其他操作
                            cancelPermissionDialog();
                        }
                    })
                    .create();
        }
        mPermissionDialog.show();
    }


    //关闭对话框
    private void cancelPermissionDialog() {
        if (mPermissionDialog != null) {
            mPermissionDialog.cancel();
            mPermissionDialog = null;
            if(mActivity != null){
                mActivity.finish();
            }
        }
    }

    //把两个数组拼接到一起
    public static String[] Splicing(String[] checks1, String[] checks2) {
        String[] checks = new String[0];
        if (checks1 == null && checks2 == null) {
            return null;
        } else if (checks1 == null) {
            checks = checks2;
        } else if (checks2 == null) {
            checks = checks1;
        } else {
            int len1 = checks1.length;
            int len2 = checks2.length;
            int len = len1 + len2;
            checks = new String[len];
            for (int i = 0; i < len; i++) {
                if (i < len1) {
                    checks[i] = checks1[i];
                } else {
                    checks[i] = checks2[i - len1];
                }
            }
        }
        return checks;
    }

    //把任意多个数组拼接到一起
    public static String[] Splicing(String[] checks1, String[]... args) {
        String[] checks = new String[0];
        if (checks1 == null && args == null) {
            return null;
        } else if (args == null) {
            checks = checks1;
        } else {
            if (checks1 != null) {
                int len1 = checks1.length;
                checks = new String[len1];
                int j = 0;
                for (; j < checks.length; j++) {
                    checks[j] = checks1[j];
                }
            }
            for (int i = 0; i < args.length; i++) {
                String[] arg = args[i];
                if (arg != null && arg.length > 0) {
                    String[] temp = checks;
                    checks = new String[checks.length + arg.length];
                    for (int k = 0; k < checks.length; k++) {
                        if (k < temp.length) {
                            checks[k] = temp[k];
                        } else {
                            checks[k] = arg[k - temp.length];
                        }
                    }
                }
            }
        }
        return checks;
    }

    public static boolean getGranted(String[] permissions, int[] grantResults) {
        boolean granted = false;
        if (permissions != null && grantResults != null && permissions.length == grantResults.length) {
            int i = 0;
            for (; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    granted = false;
                    break;
                }
            }
            if (i == grantResults.length) {
                granted = true;
            }
        }
        return granted;
    }


}

 

 

MainActivity:

package top.lyoun.myusb;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("版本号: " + android.os.Build.VERSION.SDK_INT);
        //权限检查,只有android6.0及其以上才需要动态权限检查
        permissionsUtil = new MyPermissionsUtil();
        String[] needCheckPermissions = MyPermissionsUtil.Splicing(MyPermissionsUtil.permissions_storage,
                MyPermissionsUtil.permissions_audio);
        //        permissionsUtil.checkPermissions(this, MyPermissionsUtil.permissions_storage,
        //                MyPermissionsUtil.requestCode_storage);

        permissionsUtil.checkMorePermissions(this,
                MyPermissionsUtil.permissions_storage,
                MyPermissionsUtil.permissions_audio,
                //                null,
                //                null,
                //                null,
                //                null,
                //                null,
                //                null,
                //                null
                MyPermissionsUtil.permissions_camera,
                MyPermissionsUtil.permissions_location,
                MyPermissionsUtil.permissions_phone,
                MyPermissionsUtil.permissions_calendar,
                MyPermissionsUtil.permissions_contacts,
                MyPermissionsUtil.permissions_sms,
                MyPermissionsUtil.permissions_sensors
        );

        File file = new File("/sdcard/test.txt");
        try {
            file.createNewFile();
            System.out.println("文件创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件创建失败!");
        }
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("1111".getBytes());
            fos.flush();
            System.out.println("文件写入成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件写入失败!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件写入失败!");
        }
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            System.out.println("bytes: " + new String(bytes));
            System.out.println("文件读取成功!");
        } catch (FileNotFoundException e) {
            System.out.println("文件读取失败!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败!");
        }
    }


    MyPermissionsUtil permissionsUtil;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean granted = false;
        switch (requestCode) {
            case MyPermissionsUtil.requestCode_storage:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_storage, grantResults);
                if (granted) {
                    if (!permissions[0].contains("_EXTERNAL_STORAGE")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("_EXTERNAL_STORAGE")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.storagesGranted = granted;
                if (!granted) {
                    permissionsUtil.storagesDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.storagesDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_audio:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_audio, grantResults);
                System.out.println("我被执行了啊啊:" + granted + " , " + requestCode + " : "
                        + permissions[0]);
                if (granted) {
                    if (!permissions[0].contains("RECORD_AUDIO")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("RECORD_AUDIO")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.audiosGranted = granted;
                if (!granted) {
                    permissionsUtil.audiosDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                    System.out.println("audioDenyed: " + permissionsUtil.audiosDenyed);
                } else {
                    permissionsUtil.audiosDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_camera:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_camera, grantResults);
                if (granted) {
                    if (!permissions[0].contains("CAMERA")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("CAMERA")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.camerasGranted = granted;
                if (!granted) {
                    permissionsUtil.camerasDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                    System.out.println("camerasDenyed: " + permissionsUtil.camerasDenyed);
                } else {
                    permissionsUtil.camerasDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }

                break;
            case MyPermissionsUtil.requestCode_location:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_location, grantResults);
                if (granted) {
                    if (!permissions[0].contains("_LOCATION")
                    ) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("_LOCATION")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.locationsGranted = granted;
                if (!granted) {
                    permissionsUtil.locationsDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                    System.out.println("locationsDenyed: " + permissionsUtil.locationsDenyed);
                } else {
                    permissionsUtil.locationsDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_phone:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_phone, grantResults);
                if (granted) {
                    if ((!permissions[0].contains("_PHONE")
                            && !permissions[0].contains("_CALL"))) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && (permissions[0].contains("_PHONE")
                        || permissions[0].contains("_CALL"))) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.phonesGranted = granted;
                if (!granted) {
                    permissionsUtil.phonesDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.phonesDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_calendar:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_calendar, grantResults);
                if (granted) {
                    if (!permissions[0].contains("_CALENDAR")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("_CALENDAR")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.calendarsGranted = granted;
                if (!granted) {
                    permissionsUtil.calendarsDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.calendarsDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_contacts:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_contacts, grantResults);
                if (granted) {
                    if (!permissions[0].contains("_CONTACTS")
                            && !permissions[0].contains("_ACCOUNTS")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && (permissions[0].contains("_CONTACTS")
                        || permissions[0].contains("_ACCOUNTS"))) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.contactsGranted = granted;
                if (!granted) {
                    permissionsUtil.contactsDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.contactsDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_sms:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_sms, grantResults);
                if (granted) {
                    if (!permissions[0].contains("_SMS")
                            && !permissions[0].contains("_WAP_PUSH")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && (permissions[0].contains("_SMS")
                        || permissions[0].contains("_WAP_PUSH"))) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.smsGranted = granted;
                if (!granted) {
                    permissionsUtil.smsDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.smsDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
            case MyPermissionsUtil.requestCode_sensors:
                granted = MyPermissionsUtil.getGranted(MyPermissionsUtil.permissions_sensors, grantResults);
                if (granted) {
                    if (!permissions[0].contains("BODY_SENSORS")) {
                        granted = false;
                    }
                }
                if (permissions != null && permissions.length > 0 && permissions[0].contains("BODY_SENSORS")) {
                    permissionsUtil.compareCount++;
                }
                permissionsUtil.sensorsGranted = granted;
                if (!granted) {
                    permissionsUtil.sensorsDenyed++;
                    System.out.println("权限不通过:" + requestCode);
                } else {
                    permissionsUtil.sensorsDenyed = 0;
                    System.out.println("权限通过:" + requestCode);
                }
                break;
        }
        System.out.println(requestCode + " : compareCount: " + MyPermissionsUtil.compareCount
                + " ,needCompareCount: " + MyPermissionsUtil.needCompareCount);
        if (!permissionsUtil.audiosGranted && permissionsUtil.checkAudios != null
                && permissionsUtil.audiosDenyed < 1) {
            System.out.println("audioDenyed我被执行力-----: " + permissionsUtil.audiosDenyed);
            ActivityCompat.requestPermissions(this, permissionsUtil.checkAudios, MyPermissionsUtil.requestCode_audio);
        }
        else if (!permissionsUtil.camerasGranted && permissionsUtil.checkCameras != null
                && permissionsUtil.camerasDenyed < 1) {
            System.out.println("camerasDenyed: " + permissionsUtil.camerasDenyed);
            ActivityCompat.requestPermissions(this, permissionsUtil.checkCameras, MyPermissionsUtil.requestCode_camera);
        } else if (!permissionsUtil.locationsGranted && permissionsUtil.checkLocations != null
                && permissionsUtil.locationsDenyed < 1) {
            System.out.println("locationsDenyed: " + permissionsUtil.locationsDenyed);
            ActivityCompat.requestPermissions(this, permissionsUtil.checkLocations, MyPermissionsUtil.requestCode_location);
        } else if (!permissionsUtil.phonesGranted && permissionsUtil.checkPhones != null
                && permissionsUtil.phonesDenyed < 1) {
            ActivityCompat.requestPermissions(this, permissionsUtil.checkPhones, MyPermissionsUtil.requestCode_phone);
        } else if (!permissionsUtil.calendarsGranted && permissionsUtil.checkCalendars != null
                && permissionsUtil.calendarsDenyed < 1) {
            ActivityCompat.requestPermissions(this, permissionsUtil.checkCalendars, MyPermissionsUtil.requestCode_calendar);
        } else if (!permissionsUtil.contactsGranted && permissionsUtil.checkContacts != null
                && permissionsUtil.contactsDenyed < 1) {
            ActivityCompat.requestPermissions(this, permissionsUtil.checkContacts, MyPermissionsUtil.requestCode_contacts);
        } else if (!permissionsUtil.smsGranted && permissionsUtil.checkSms != null
                && permissionsUtil.smsDenyed < 1) {
            ActivityCompat.requestPermissions(this, permissionsUtil.checkSms, MyPermissionsUtil.requestCode_sms);
        } else if (!permissionsUtil.sensorsGranted && permissionsUtil.checkSensors != null
                && permissionsUtil.sensorsDenyed < 1) {
            ActivityCompat.requestPermissions(this, permissionsUtil.checkSensors, MyPermissionsUtil.requestCode_sensors);
        }

        //所有权限都判断完之后,再判断下有哪些关键权限,必须要授权的,却没有授权,提示用户,必须打开权限

        if ((MyPermissionsUtil.compareCount == MyPermissionsUtil.needCompareCount)
                && MyPermissionsUtil.mustGrantedPermiss != null
                && MyPermissionsUtil.mustGrantedPermiss.length > 0) {
            boolean needTip_storage = false;
            boolean needTip_audio = false;
            boolean needTip_camera = false;
            boolean needTip_location = false;
            boolean needTip_phone = false;
            boolean needTip_calendar = false;
            boolean needTip_contact = false;
            boolean needTip_sms = false;
            boolean needTip_sensor = false;

            for (int i = 0; i < MyPermissionsUtil.mustGrantedPermiss.length; i++) {
                String permission = MyPermissionsUtil.mustGrantedPermiss[i];
                if (permission.contains("_EXTERNAL_STORAGE")
                        && !permissionsUtil.storagesGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_storage = true;
                }

                if (permission.contains("RECORD_AUDIO")
                        && !permissionsUtil.audiosGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_audio = true;
                }

                if (permission.contains("CAMERA")
                        && !permissionsUtil.camerasGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_camera = true;
                }

                if (permission.contains("_LOCATION")
                        && !permissionsUtil.locationsGranted
                        ) {
                    needTip_location = true;
                    for(int j = 0; j < MyPermissionsUtil.permissions_location.length;j++){
                        if(checkSelfPermission(MyPermissionsUtil.permissions_location[j]) == MyPermissionsUtil.granted){
                            needTip_location = false;
                            break;
                        }
                    }
                }

                if ((permission.contains("_PHONE")
                        || permission.contains("_CALL"))
                        && !permissionsUtil.phonesGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted
                ) {
                    needTip_phone = true;
                }

                if (permission.contains("_CALENDAR")
                        && !permissionsUtil.calendarsGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_calendar = true;
                }

                if ((permission.contains("_CONTACTS")
                        || permission.contains("_ACCOUNTS"))
                        && !permissionsUtil.contactsGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_contact = true;
                }

                if ((permission.contains("_SMS")
                        || permission.contains("_WAP_PUSH"))
                        && !permissionsUtil.smsGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_sms = true;
                }

                if (permission.contains("BODY_SENSORS")
                        && !permissionsUtil.sensorsGranted
                        && checkSelfPermission(permission) != MyPermissionsUtil.granted) {
                    needTip_sensor = true;
                }
            }
            System.out.println("needTips: " + needTip_storage + ","
                    + needTip_audio + ","
                    + needTip_camera + ","
                    + needTip_location + ","
                    + needTip_phone + ","
                    + needTip_calendar + ","
                    + needTip_contact + ","
                    + needTip_sms + ","
                    + needTip_sensor);
            StringBuffer sb = new StringBuffer();
            if (needTip_storage) {//本App用到权限,非常重要,为保证app能够正常使用,请在应用程序权限设置界面,授予该权限
                sb.append("STORAGE 存储卡文件读写权限、");
            }
            if (needTip_audio) {
                sb.append("MICROPHONE 麦克风录音权限、");
            }
            if (needTip_camera) {
                sb.append("CAMERA 相机权限、");
            }
            if (needTip_location) {
                sb.append("LOCATION 位置权限、");
            }
            if (needTip_phone) {
                sb.append("PHONE 手机权限、");
            }
            if (needTip_calendar) {
                sb.append("CALENDAR 日历权限、");
            }
            if (needTip_contact) {
                sb.append("CONTACTS 联系人权限、");
            }
            if (needTip_sms) {
                sb.append("SMS 短信权限、");
            }
            if (needTip_sensor) {
                sb.append("BODY_SENSORS 传感器权限、");
            }
            if (sb != null && sb.length() > 0) {
                String tips = sb.toString();
                tips = tips.substring(0, tips.length() - 1);
                tips = "已禁用 " + tips + " ,为保证本App相关功能都能够正常执行,保证程序正常运行,请在系统的应用的权限设置界面上勾选相应权限!";
                permissionsUtil.showSystemPermissionsSettingDialog(tips);
            }
        }
    }
}

在Android Q中引入了分区储存功能,在外部存储设备中为每个应用提供了一个“隔离存储沙盒”。其他应用无法直接访问应用的沙盒文件。由于文件是应用的私有文件,不再需要任何权限即可访问和保存自己的文件。此变更并有助于减少应用所需的权限数量,同时保证用户文件的隐私性。Android Q 更改了应用对设备外部存储设备中的文件(如:/sdcard )的访问方式。

继续使用 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限,只不过当拥有这些权限的时候,你只能访问媒体文件,无法访问其他文件。如果Target SDK > 28,请在manifest中添加android:requestLegacyExternalStorage=“true”

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值