RuntimePermissions

这只是我在学Android过程中对于所学知识的巩固和方便日后查询的学习笔记,能帮助到有需要的和我一样的初学者就更好了

android6.0引入,为避免软件开发方恶意申请权限,引入运行时权限在需要权限时申请并由用户决定是否授权。即使不予授权也只会影响该应用个别功能而不影响其他功能

需要申请的危险权限

GroupName PermissionName
CALENDAR READ_CALENDAR
WRIET_CALENDER
CAMERA CAMERA
CONTACTS READ_CONTACTS
WRITE_CONTACTS ;GET_CPNTACTS
LOCATION ACESS_FINAL_LOCATION
ACESS_COARSE_LOCATION
MICROPHONE RECORD_AUDIO
PHONE READ_PHONE_STATE
CALL_PHONE
READ_ALL_LOG
WRITE_ALL_LOG
ADD_VOICEMAIL
USE_SIP
SENSORS BODY_SENSORS
SMS SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE READ_EXTERNAL_STORAGE
WRITE_EXRETNAL_STORAGE

其余的权限在AndrodiManifest.xml中即可

运行时申请权限

以MainActivity中申请CALL_PHONE为例

if(ContextCompat.checkselfpermission(MainActivity.this ,Manifest.permission.CALL_PHONE))
            !=PackageManager.PERMISSION_GRANTED){
//自查权限是否与已申请
    ActivityCompat.requestPermission(MainActivity.this , new String[]{Manifest.permission.CALL_PHONE} ,1);
//三个参数为context、权限名字字符串数组、请求码(可自定但要唯一)
}

弹出的对话框不论选择了ALLOW还是DENY都会回调以下方法

@override
public void onRequestPermissionResult(int requestCode ,String[] permissions 
                                                                                           ,int[] grantResults ){
    switch(requestCode){
        case 1:               //前面自定的请求码
            if(grantResules.length()>0&&grantResults[0]==
                                    PackageManager.PERMISSION_GRANTER){
                //表示通过申请,可执行接下来的逻辑
            }else{
                //添加申请未通过的回执,例如Toast
           }
            break;
        default :
            break;
    }
}

要注意的是,如果该权限所属的权限组中还有其他权限的话,同意此权限也会同时授权其所属权限组的其他权限并且用户不手动关闭的话会一直保持授权

同时申请多个权限

只要先判断是否已申请通过,若没通过则添加至权限名字符串数组中去申请权限即可

List permissionList=new ArrayList();
//保存要申请的权限名称的集合
if(ContextCompat.checkselfpermission(MainActivity.this ,Manifest.permission.CALL_PHONE))
            !=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.CALL_PHONE);
}
if(ContextCompat.checkselfpermission(MainActivity.this ,Manifest.permission.READ_PHONE_STATE))
            !=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if(ContextCompat.checkselfpermission(MainActivity.this ,Manifest.permission.ACCESS_FINAL_LOCATION))
            !=PackageManager.PERMISSION_GRANTED){
    permissionList.add(Manifest.permission.ACCESS_FINAL_LOCATION);
}
if(!permissionList.isEmpty()){
    String[] permissions=permissionList.toArray(new String[permissionList.size()]);
    //将保存了权限名字的集合转为数组形式
    ActivityCompat.requestPermissions(MainActivity.this ,permissions ,1);
}

@override
public void onRequestPermissionResult(int requestCode ,String[] permissions 
                                                                                           ,int[] grantResults ){
    switch(requestCode){
        case 1:              
            if(grantResules.length()>0){
                for(int result :grantResults){
                    if(result!=PackageManager.PERMISSION_GRANTED){
                        //没通过申请,添加回执例如Toast
                    }
                }
            }else{
              //通过申请,执行所需代码
             }
             break;
        default:
             break;
    }

总之就是申请的结果保存在了int[] grantResults中,不论有多少以此取出和
PackageManager.PERMISSION_GRANTED对比,相同为通过,不同为未通过

你可能感兴趣的:(RuntimePermissions)