final private int REQUEST_CODE_ASK_PERMISSIONS = 123 ;
try {
int hasWriteContactsPermission =context.checkSelfPermission(Manifest.permission.CAMERA);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS);
return ;
}else {
}
} catch (Exception e) {
e.printStackTrace();
ToastUtil.defaultToast(getContext().getApplicationContext(), "权限异常" );
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0 ] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(getActivity(),
CaptureActivity.class );
startActivityForResult(intent, REQUEST_CODE_SCAN);
} else {
ToastUtil.defaultToast(context.getApplicationContext(),"获取权限失败,无法扫描" );
}
break ;
default :
super .onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}