动态获取Android权限

时间:2022-06-09 13:23:48
@TargetApi(23)
private void showPhotoDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
} dialog = new BottomMenuDialog(mContext);
dialog.setConfirmListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
//如果sdk版本大于23 需要动态获取权限
if (Build.VERSION.SDK_INT >= 23) {
int checkPermission = checkSelfPermission(Manifest.permission.CAMERA);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS);
} else {
new AlertDialog.Builder(mContext)
.setMessage("您需要在设置里打开相机权限。")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS);
}
})
.setNegativeButton("取消", null)
.create().show();
}
return;
}
}
photoUtils.takePicture(MyAccountActivity.this);
}
});
//选择照片
dialog.setMiddleListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
photoUtils.selectPicture(MyAccountActivity.this);
}
});
dialog.show();
}