//所需要申请的权限数组
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);
}
}
网友评论