应用常量
public static final String CAMERA_PERMISSION = CAMERA;
public static final String READ_STORAGE_PERMISSION = READ_EXTERNAL_STORAGE;
public static final String WRITE_STORAGE_PERMISSION = WRITE_EXTERNAL_STORAGE;
public static final String LOCATION_PERMISSION = ACCESS_FINE_LOCATION;
public static final String READ_PHONE_STATE_PERMISSION= READ_PHONE_STATE;
从您的(活动或片段或按钮)调用此函数以检查权限
//Getting permission here
public boolean needPermission() {
try {
int readPhoneState = ContextCompat.checkSelfPermission(this, AppConstants.READ_PHONE_STATE_PERMISSION);
int location = ContextCompat.checkSelfPermission(this, AppConstants.LOCATION_PERMISSION);
if (readPhoneState == PackageManager.PERMISSION_GRANTED && location == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
AppGlobal.checkSinglePermission(this, AppConstants.READ_PHONE_STATE_PERMISSION);
} else if (location != PackageManager.PERMISSION_GRANTED) {
AppGlobal.checkSinglePermission(this, AppConstants.LOCATION_PERMISSION);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
此功能将检查权限类型
//Check single permission
public static boolean checkSinglePermission(final Context context, String permissionType) {
ArrayList<String> permissionList = new ArrayList<>();
String type = "";
if (permissionType != null) {
if (permissionType.equals(AppConstants.CAMERA_PERMISSION)) {
type = permissionType;
}
if (permissionType.equals(AppConstants.READ_STORAGE_PERMISSION)) {
type = permissionType;
}
if (permissionType.equals(AppConstants.WRITE_STORAGE_PERMISSION)) {
type = permissionType;
}
if (permissionType.equals(AppConstants.READ_PHONE_STATE_PERMISSION)) {
type = permissionType;
}
if (permissionType.equals(AppConstants.LOCATION_PERMISSION)) {
type = permissionType;
}
int getPermission = ContextCompat.checkSelfPermission(context, type);
if (getPermission != PackageManager.PERMISSION_GRANTED) {
permissionList.add(type);
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions((Activity) context, permissionList.toArray
(new String[permissionList.size()]), SINGLE_PERMISSION_REQUEST);
return false;
}
}
}
return true;
}
然后你会在这个onRequestPermissionsResult()中得到权限成功和失败的响应
//Permission request result here
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (String permission : permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
AppLog.e("denied" + permission);//denied
needPermission();//needed permission for getting Country code
} else {
if (ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
AppLog.e("allowed" + permission);//allowed
needPermission();//set data
} else {
//set to never ask again
AppLog.e("set to never ask again" + permission);
AppDialog.showAlertDialog(this, null,
getString(R.string.read_phone_state_permission_txt), getString(R.string.txt_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
AppGlobal.permissionSettingView(LoginActivity.this);
}
});
}
}
}
}