Android6.0以上权限管理问题(权限被拒绝后)

          写之前,先发个牢骚,特么天天加班,感觉身体被掏空了........................

         -------------------------------------------------------------------------------------------------------------------------

          回到正题,对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装(比如是个app都要访问通讯录、短信,SD卡,相机等)。而在6.0以后,我们可以直接安装,当app需要我们授予不恰当的权限的时候,我们可以予以拒绝(比如:获取短信,SD卡,我一般都是拒绝的)。当然你也可以在设置界面对每个app的权限进行查看,以及对单个权限进行授权或者解除授权。Android6.0添加了运行时权限,用户可以在已安装的app上立刻同意或者拒绝权限,那么就意味着我们在使用APP的时候,当一个app会使用一些危险权限的时候就会提示用户是否授权.新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。

那么这个权限机制对于用户来说是挺好的,但是对于我们开发人员来讲,却又多了一种适配-权限适配.

就拿申请访问SD卡来说吧,如果用户在第一次弹出授权申请的时候,用户选择了拒绝并不再提示,那么我们访问SD卡的路径就会出错.那么怎么去避免出错呢.

我的解决思路是,先判断权限,再去决定是否响应事件.然后根据回调函数判断是否授权.未授权弹出dialog提示用户去开启权限.

判断当前版本是否为6.0及以上(在清单文件中已经申请权限了)

 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
                   ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            PermissionsUtils.REQUEST_EXTERNAL_STORAGE);
               }else {
                    //正常响应事件
               }
回调函数

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

            switch (requestCode){
                case PermissionsUtils.REQUEST_EXTERNAL_STORAGE:

                    if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                        //已获取权限
                  
                        //响应事件
                    }else{
                        //权限被拒绝
                       
                        new SimpleBaseDialog(this, "需要开启权限后才能使用", "去设置", "取消", new SimpleClickListener() {
                        @Override
                        public void ok() {
                            Intent intent=CMethod.getAppDetailSettingIntent(PulishWriteActivity.this);
                            startActivity(intent);
                        }
                        @Override
                        public void cancle() {

                        }
                    }).show();
                    }

                    break;
           }    
 /**
     * 获取应用详情页面intent
     *
     * @return
     */
    public static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }
最后附上一个工具类

public class PermissionUtils {

    public static final int MY_PERMISSIONS_REQUEST_CODE=10001;

    public static boolean checkAudioPermission(Context context){  //检查麦克风权限 ,写入文件,读出文件权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            //申请RECORD_AUDIO  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请RECORD_AUDIO  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请RECORD_AUDIO  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            //权限已经允许   请执行下一步操作
//            Toast.makeText(context,"Permission Granted",Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }

    public static boolean checkCameraPermission(Context context){  //检查拍照权限 ,写入文件,读出文件权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            return true;
        }
        return false;
    }

    public static boolean checkCameraScanPermission(Context context){  //检查拍照权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            return true;
        }
        return false;
    }

    public static boolean checkExternalStoragePermission(Context context){  //检查写入文件,读出文件权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //CAMERA  WRITE_EXTERNAL_STORAGE  READ_EXTERNAL_STORAGE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            return true;
        }
        return false;
    }

    public static boolean checkLocationPermission(Context context){  //检查获取位置权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            //ACCESS_COARSE_LOCATION  ACCESS_FINE_LOCATION 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            //ACCESS_COARSE_LOCATION  ACCESS_FINE_LOCATION 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            //权限已经允许   请执行下一步操作
//            Toast.makeText(context,"Permission Granted",Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }

    public static boolean checkContactsPermission(Context context){  //检查获取联系人权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            //READ_CONTACTS  GET_ACCOUNTS 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED){
            return true;
        }
        return false;
    }

    public static boolean checkSendSMSPermission(Context context){  //检查获取发短信权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            //SEND_SMS 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            //权限已经允许   请执行下一步操作
//            Toast.makeText(context,"Permission Granted",Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }

    public static boolean checkPhonePermission(Context context){  //检查获取电话权限
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            //READ_PHONE_STATE  CALL_PHONE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //READ_PHONE_STATE  CALL_PHONE 权限
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CODE);
        }else{
            //权限已经允许   请执行下一步操作
//            Toast.makeText(context,"Permission Granted",Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }}

that's all THX











            
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dailog/article/details/70159297
文章标签: Android Permission
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Android6.0以上权限管理问题(权限被拒绝后)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭