6.0动态申请权限

https://www.cnblogs.com/dubo-/p/6018262.html
详情:https://blog.csdn.net/xietansheng/article/details/54315674
首先 :设置要添加的权限,这里我们添加两个权限:1.照相机权限 2.SD卡权限
定义一个数组
String permissions[] = {Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE}; //动态添加权限

image.png

···
public void checkPermission(){
//判断当前系统版本号是否符合要求 6.0
if(Build.VERSION.SDK_INT >=23){
for(String permission : permissions){
//判断遍历的权限是否已被申请
//PERMISSION_GRANTED 同意申请
if(checkSelfPermission(permission)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{permission},100);
}

        }
    }

}

···
重写onRequestPermissionsResult方法
···
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode == 100){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "获取权限:"+permissions[0], Toast.LENGTH_SHORT).show();
}
}
}
···

你可能感兴趣的:(6.0动态申请权限)