android动态申请权限 发表于 2019-07-23 | 分类于 android 字数统计: 254 word | 阅读时长 ≈ 1 min 申请动态权限 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 //声明一个权限数组private String[] permissions = new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};private List<String> mPermissionList = new ArrayList<>();//定义一个请求码private static final int PERMISSION_REQUEST = 1;//动态申请权限方法private void initPermission(){ mPermissionList.clear(); /** * 判断哪些权限未授予 */ for (int i = 0; i < permissions.length; i++) { if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { mPermissionList.add(permissions[i]); } } /** * 判断是否为空 */ if (mPermissionList.isEmpty()) { //未授予的权限为空,表示都授予了 } else { //请求权限方法 String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组 //调用requestPermissions()向系统申请权限 //该方法接收3个参数,第一个是activity实例,第二个是一个String数组,第三个是请求码 ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST); } } /** * 响应授权 * 这里不管用户是否拒绝,都进入首页,不再重复申请权限 */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case PERMISSION_REQUEST: break; default: break; } }