美文网首页
8.0上多个权限批量申请

8.0上多个权限批量申请

作者: reader_b8f9 | 来源:发表于2018-04-12 13:57 被阅读0次
    //所需要申请的权限数组
        private static final String[] permissionsArray = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
        };
        //还需申请的权限列表
        private List<String> permissionsList = new ArrayList<String>();
        //申请权限后的返回码
        private static final int REQUEST_CODE_ASK_PERMISSIONS = 1;
    
    
        private void checkRequiredPermission(final Activity activity) {
            for (String permission : permissionsArray) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionsList.add(permission);
                } else {
    
                }
            }
            //申请权限
            if(permissionsList.size()!=0){
    
            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_PERMISSIONS);
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case REQUEST_CODE_ASK_PERMISSIONS:
                    for (int i = 0; i < permissions.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
    
                        } else {
                            Toast.makeText(getApplicationContext(), "权限被拒绝: " + permissions[i], Toast.LENGTH_SHORT).show();
                        }
                    }
                    initPdf();
                    break;
                default:
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }    
    

    相关文章

      网友评论

          本文标题:8.0上多个权限批量申请

          本文链接:https://www.haomeiwen.com/subject/ugbxkftx.html